how can I handle message box reply example if the user click on yes do something if the user click on NO do another thing ?
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
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
}
Here is an example:
DialogResult userSelection = MessageBox.Show("Some question","Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
// Do something with userSelection
DialogResult result = MessageBox.Show("Some Text", "Title", MessageBoxButtons.YesNoCancel);
if(result == DialogResult.Yes)
{
// do something
}
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");
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.