while using the MessageBox in C# we can control what the app will do if the end-user clicked "Yes", "No", or "Cancel" labeled buttons, As follow:
DialogResult overwriteDialog = WinForms.MessageBox.Show("Do you want to overwrite?", "Overwriting", MessageBoxButtons.YesNoCancel);
if (overwriteDialog == DialogResult.Yes)
{
#do something
}
else if (overwriteDialog == DialogResult.No || overwriteDialog == DialogResult.Cancel)
{
#do something else
}
The question here is how to have another else if statement to make the code do something else if the close [X] button is clicked ?
it seems like we can't return a value if the user clicked on close[X] button, but we can simply add extra else statement since that would be the last option after constraining all the other buttons in the if statements.
Credits for answer to @preciousbetine and @SLaks
I think you will get value DialogResult.None for message box when you will click red close button. Despite of it you can provide default value for MessageBox by one of parameters ( https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox.show?view=netframework-4.7.2#System_Windows_Forms_MessageBox_Show_System_Windows_Forms_IWin32Window_System_String_System_String_System_Windows_Forms_MessageBoxButtons_System_Windows_Forms_MessageBoxIcon_System_Windows_Forms_MessageBoxDefaultButton_System_Windows_Forms_MessageBoxOptions_System_String_ ). If default is specified you will get default value instead of None value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.