Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handle reply from message box c#

Tags:

c#

wpf

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 ?

like image 309
kartal Avatar asked Oct 15 '10 20:10

kartal


People also ask

How to show a MessageBox in c#?

You cannot create a new instance of the MessageBox class. To display a message box, call the static method MessageBox. Show. The title, message, buttons, and icons displayed in the message box are determined by parameters that you pass to this method.

What is the message box?

A message box is a special dialog box used to display a piece of information to the user. As opposed to a regular form, the user cannot type anything in the dialog box. To support message boxes, the Visual Basic language provides a function named MsgBox.

How do I open a message box?

Open Settings . Click Apps. In the list of apps, click Messages.

How to display message box in WPF?

The following line of code uses the Show method to display a message box with a simple message: MessageBoxResult result = MessageBox. Show("Hello MessageBox");


1 Answers

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
}
like image 170
eldarerathis Avatar answered Sep 30 '22 04:09

eldarerathis