简体   繁体   中英

How to return a value when the Close button [X] is clicked using the MessageBox? (C#)

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM