开发者

handle reply from message box c#

how can I handle mes开发者_JS百科sage box reply example if the user click on yes do something if the user click on NO do another thing ?


Example (slightly modified) from the docs:

const string message =
    "Are you sure that you would like to close the form?";
const string caption = "Form Closing";
var result = MessageBox.Show(message, caption,
                             MessageBoxButtons.YesNo,
                             MessageBoxIcon.Question);

// If the no button was pressed ...
if (result == DialogResult.No)
{
    //Do something for No
} 
else if (result == DialogResult.Yes) 
{
    //Do something else for Yes
}

Addendum: In the event that you're still on .NET 2.0 and don't have access to the var keyword, declare result as a DialogResult. I.e:

DialogResult result = MessageBox.Show(...);

Missed the fact that this was tagged with WPF, so if you're using that then you'd be using the slightly (but not too much) different System.Windows.MessageBox class instead of System.Windows.Forms.Messagebox. The interaction is largely the same, but also uses the MessageBoxResult enum instead of DialogResult, the MessageBoxImage enum instead of MessageBoxIcon, and the MessageBoxButton enum instead of MessageBoxButtons (plural). You should be able to do something like this:

const string message =
        "Are you sure that you would like to close the form?";
const string caption = "Form Closing";
MessageBoxResult result = MessageBox.Show(message, caption,
                                 MessageBoxButton.YesNo,
                                 MessageBoxImage.Question);

if (result == MessageBoxResult.No)
{
    // Do something for No
}
else if (result == MessageBoxResult.Yes)
{
    // Do something else for Yes
}


Since the tag states WPF and NOT WinForms, you will need to do something like this for a MessageBox:

MessageBoxResult result = MessageBox.Show("Foo Bar?", "Confirmation", MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Yes)
{
    // yeah yeah yeah stuff
}
else if (result == MessageBoxResult.No)
{
    // no no no stuff
}
else
{
    // forget about it
}

In addition the dialogs are dealt with differently in WPF as well, note the absence of DialogResult:

SomeDialog dialog = new SomeDialog();
dialog.ShowDialog();

if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
     MessageBox.Show("Clicked ok");
else
     MessageBox.Show("Clicked cancel");


You should try using google or msdn (the links are clickable).

Anyways, you should check the value of the messageboxresult returned by the show method. http://msdn.microsoft.com/en-us/library/ms598674.aspx


The best for me is

if (MessageBox.Show("Are you sure you want to close the window ?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
        {
            //do NO stuff
        }
        else
        {
            //do YES stuff
        }


DialogResult result = MessageBox.Show("Some Text", "Title", MessageBoxButtons.YesNoCancel);

if(result == DialogResult.Yes)
{
    // do something
}


Here is an example:

DialogResult userSelection = MessageBox.Show("Some question","Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question);

// Do something with userSelection   


childwindow's in WPF are asynchronous actions. You have to handle the Close event, and inside your close event then you can perform your logic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜