2

I writing an application in C# in Visual Studio 2017. I'm using Windows Forms App (.NET Framework). I have a MessageBox pop up with its default settings (only the OK button and the X in the top right corner). When the user selects "OK" the remaining code resumes. I want to write separate code to run when the user selects the X to close the message box. How can I tell if the user has clicked the X to close the message box?

I have tried using

DialogResult result = MessageBox.Show("Message here");
if(result != DialogResult.OK){
    //Do stuff here
} 

but even when the X is pressed, result still comes back as Dialog.OK.

What should I do?

Update

This code works fine

DialogResult result = MessageBox.Show("Message here", "MessageBoxTitle", MessageBoxButtons.OKCancel);
if(result != DialogResult.OK){
    //Do stuff here
} 

However, my message box now has an unnecessary Cancel button. Is there a way to achieve this with just the MessageBoxButtons.OK setting so that I avoid having the Cancel button?

Susi
  • 63
  • 7
  • 1
    The API you are using (WinForm, ASP.NET, WPF, UWP) is more important to know than what language or IDE you are using. – Dour High Arch Dec 03 '19 at 21:12
  • That code should work.... – Isma Dec 03 '19 at 21:13
  • Use debugger to see what the value is at the if statement – Jawad Dec 03 '19 at 21:14
  • Okay. I'm using Windows Forms App (.NET Framework). – Susi Dec 03 '19 at 21:14
  • @MarcusLai, I used the debugger and it said result has the value OK. – Susi Dec 03 '19 at 21:16
  • Is there any reason not to specify MessageBox Options to use Ok/Cancel? Then non-"okay" messages default to cancel I believe. – Ethan DeLong Dec 03 '19 at 21:25
  • @EthanDeLong When I used the default MessageBoxButtons.OK, clicking on the X resulted in a dialog response of DialogResult.OK. When I use MessageBoxButtons.OKCancel, clicking on the X resulted in a dialog response of DialogResult.Cancel. However, I do not want the extra Cancel button on the message box if it can be avoided. – Susi Dec 03 '19 at 21:36

2 Answers2

4

This is a limitation of the underlying Win32 MessageBox API.

The API does not provide a way to specify how the Close box works separately. Clicking the Close box (or pressing Escape) always returns the ID of the Cancel button if there is one, or the default button if there isn't.

And unfortunately, no, you can't try to cheat by setting the default button to a nonexistent button -- if you do that, the default will be reset to one of the existing buttons.

So if you want to handle the Close box in a more sophisticated way than that, you'll have to create your own dialog box rather than letting ::MessageBox do it for you.

  • Okay, thank you for the detailed explanation! I'll stick to my work-around and leave the cancel button for now. – Susi Dec 03 '19 at 21:45
1

In addition to the answer about Windows API.

System.Windows.Forms.MessageBox.Show("Message") internally calls private method System.Windows.Forms.ShowCore(...).

Method .Show(text) is defined in that way:

/// <summary>
///  Displays a message box with specified text.
/// </summary>
public static DialogResult Show(string text)
{
    return ShowCore(null, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
}

It means that .Show(text) is just a shorthand version of full method call. Therefore, you can achieve only those results that could be achieved by calling the actual .ShowCore(...) method.

Vlad DX
  • 4,200
  • 19
  • 28