#196 – Getting a Result Back from a MessageBox

The MessageBox.Show method always returns a result, indicating which button a user clicked to dismiss the dialog box.  If you choose, you can check the result to see which button the user clicked.  The value returned will always be a member of the MessageBoxResult enumeration.

            // Not checking result
            MessageBox.Show("Hi!");

            // Always resturns MessageBoxResult.OK, since the dialog only has one button
            MessageBoxResult res = MessageBox.Show("Hello again");

In the above examples, the result didn’t really matter.  But you often want to know which of two or more buttons the user clicked.

            MessageBoxResult res = MessageBox.Show("Do you like brussel sprouts?", "Green Food", MessageBoxButton.YesNo);

            if (res == MessageBoxResult.No)
                MessageBox.Show("Me neither");
            else
                MessageBox.Show("Wow, you're a cruciferous vegetable lover!");
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: