C# WPF Tutorial – MessageBoxButton

0
174
C# WPF Tutorial – MessageBoxButton

A WPF (Windows Presentation Foundation) MessageBoxButton is an enumeration that specifies the buttons that are displayed in a message box. The possible values for a MessageBoxButton are:

  • OK
  • OKCancel
  • YesNo
  • YesNoCancel

These values are used as the parameter for the MessageBoxButton property of the MessageBox class, which determines which buttons are displayed in the message box.

For example, if you set the MessageBoxButton property to YesNo, the message box will display “Yes” and “No” buttons. The user’s response to the message box can be retrieved using the MessageBoxResult property, which returns a value indicating which button the user clicked.

This allows you to take appropriate action based on the user’s response. For example, if the user clicks “Yes”, you could save the changes they made, and if they click “No”, you could discard the changes.

Here is an example of how to use the MessageBoxButton property in WPF code:

// Open a message box with "Yes" and "No" buttons
MessageBoxResult result = MessageBox.Show("Do you want to save your changes?", "Save Changes", MessageBoxButton.YesNo);

// Check the user's response and take appropriate action
if (result == MessageBoxResult.Yes)
{
    // Save the changes
    // ...
}
else if (result == MessageBoxResult.No)
{
    // Discard the changes
    // ...
}

In this example, we use the MessageBox.Show method to display a message box with the text “Do you want to save your changes?” and the title “Save Changes”.

C# WPF Tutorial – MessageBoxButton Example

We set the MessageBoxButton property to YesNo, which causes the message box to display “Yes” and “No” buttons. When the user clicks one of these buttons, the MessageBoxResult property returns a value indicating which button was clicked.

We can then use an if statement to check the value of the MessageBoxResult and take appropriate action based on the user’s response.

If you have any kind of questions left, feel free to contact me. Did you know how to use WPF DatePicker? it might be useful for you.