#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

            // 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");
                MessageBox.Show("Wow, you're a cruciferous vegetable lover!");