Jak utworzyć okno wiadomości z opcjami "tak", " nie " i DialogResult?

Chcę zrobić prosty Yes / No choiced MessageBox, ale myślę, że nonsensem jest zaprojektowanie formularza do tego. Myślałem, że mogę użyć MessageBox, dodać przyciski, itp. aby to osiągnąć. Jest to proste, ale ponieważ nie ma DialogResult zwrócone, jak odzyskać wynik?

Author: Peter Mortensen, 2010-06-14

9 answers

To powinno wystarczyć:

DialogResult dialogResult = MessageBox.Show("Sure", "Some Title", MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
    //do something
}
else if (dialogResult == DialogResult.No)
{
    //do something else
}
 686
Author: Mikael Svenson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-14 11:37:33
DialogResult dr = MessageBox.Show("Are you happy now?", 
                      "Mood Test", MessageBoxButtons.YesNo);
switch(dr)
{
   case DialogResult.Yes:
      break;
   case DialogResult.No:
      break;
}

MessageBox klasa jest tym, czego szukasz.

 54
Author: SwDevMan81,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-05 13:00:38
MessageBox.Show(title, text, messageboxbuttons.yes/no)

To zwraca DialogResult, który możesz sprawdzić.

Na przykład,

if(MessageBox.Show("","",MessageBoxButtons.YesNo) == DialogResult.Yes)
{
   //do something
}
 28
Author: Ben Cawley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-05 13:01:54

MessageBox Czy produce a DialogResults

DialogResult r = MessageBox.Show("Some question here");

Możesz również łatwo określić przyciski. Więcej dokumentacji można znaleźć na stronie http://msdn.microsoft.com/en-us/library/ba2a6d06.aspx

 16
Author: David,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-14 11:34:02

Użycie:

MessageBoxResult m = MessageBox.Show("The file will be saved here.", "File Save", MessageBoxButton.OKCancel);
if(m == m.Yes)
{
    // Do something
}
else if (m == m.No)
{
    // Do something else
}

MessageBoxResult jest używany w Windows Phone zamiast DialogResult...

 11
Author: Khateeb321,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-01 18:31:03

Możesz również użyć tego wariantu z ciągami tekstowymi, Oto kompletny zmieniony kod (kod z Mikaela), przetestowany w C # 2012:

// Variable
string MessageBoxTitle = "Some Title";
string MessageBoxContent = "Sure";

DialogResult dialogResult = MessageBox.Show(MessageBoxContent, MessageBoxTitle, MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
    //do something
}
else if (dialogResult == DialogResult.No)
{
    //do something else
}

Możesz po

.YesNo

Wstaw ikonę wiadomości

, MessageBoxIcon.Question
 6
Author: user2422690,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-26 20:00:00

@Mikael Svenson odpowiedź jest prawidłowa. Chciałem tylko dodać do niego mały dodatek:

Ikona Messagebox może być również dołączona ma dodatkową właściwość jak poniżej:

DialogResult dialogResult = MessageBox.Show("Sure", "Please Confirm Your Action", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
 5
Author: Alston Antony,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-01 18:33:01
dynamic MsgResult = this.ShowMessageBox("Do you want to cancel all pending changes ?", "Cancel Changes", MessageBoxOption.YesNo);

if (MsgResult == System.Windows.MessageBoxResult.Yes)
{
    enter code here
}
else 
{
    enter code here
}

Sprawdź więcej szczegółów z Tutaj

 0
Author: Harsh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-15 19:35:59

Ten prosty kod zadziałał dla mnie. Złapałem go z MSDN tutaj:

Https://social.msdn.microsoft.com/Forums/en-US/d1092a96-96b0-4ca4-b716-0c8e55e42ee9/how-can-i-manage-messagebox-result-?forum=Vsexpressvcs

if (System.Windows.Forms.MessageBox.Show
            ("Are you sure you want to add the audit?", "Add",
            System.Windows.Forms.MessageBoxButtons.YesNo, 
            System.Windows.Forms.MessageBoxIcon.Question)
            ==System.Windows.Forms.DialogResult.Yes)                
        // Do stuff after 'YES is clicked'
        else
        // DO stuff after 'NO is clicked'
 0
Author: Kyle Julé,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-24 14:54:48