YesNo MessageBox not closing when x-button clicked

10,982

Solution 1

The Close button (in MsgBox's title bar): Since the MsgBox window is a built-in feature of the operating system, its X button is enabled only when certain buttons are present. If there is only an OK button, clicking the X button is the same as pressing OK. Otherwise, the X button is disabled unless there is a Cancel button, in which case clicking the X is the same as pressing Cancel.

http://www.autohotkey.com/docs/commands/MsgBox.htm

Its the default behavior! from the time it was MsgBox to the time its MessageBox!

Solution 2

When you delete files in explorer (when delete confirmation is checked in recycle bin properties), a message box appears and asks "Are you sure you want to move these x items to recycle bin?" with a caption "Delete confirmation" and X at right corner. Escape also works. Buttons available are "yes" and "no" only. How can we get this done?

Either Microsoft is not following their on guidelines or not making this available for the developer.

Solution 3

Check out this CodeProject article, which outlines spinning your own MessageBox class. There's a section on disabling the close button.

Share:
10,982
Simon D.
Author by

Simon D.

Updated on June 15, 2022

Comments

  • Simon D.
    Simon D. almost 2 years

    When I open a MessageBox with options YesNo, the (usually) cancelling cross in the upper right is shown but has no effect.

    System.Windows.MessageBox.Show("Really, really?", "Are you sure?", MessageBoxButton.YesNo);
    

    If I offer YesNoCancel as options, clicking the cross closes the Dialog with DialogResult Cancel.

    System.Windows.MessageBox.Show("Really, really?", "Are you sure?", MessageBoxButton.YesNoCancel);
    

    I would have expected that the cross is "looking disabled" if not hidden at all, when clicking it has no effect. Probably I am not the first one observing this. What is your favorite way to hide/disable this button or workaround the issue?

    Note: I would prefer a solution that does not use System.Windows.Forms, since I am dealing with WPF projects and would like to avoid any InterOp if possible.

  • Simon D.
    Simon D. over 14 years
    Thank you, interesting article. Actually, I am hoping for a solution where I don't need to replace the entire MessageBox. The article seems to deal with the System.Windows.Forms.MessageBox - which I try to avoid in pure WPF projects (I will add this to the question). Still, if I find no other way I will think about using this MessageBoxExtension or transfer some of its features to some non-WinForms MessageBox extension.
  • C-Pound Guru
    C-Pound Guru over 14 years
    For years, a very common request is to be able to customize the system MessageBox--whether it's to remove/disable the close button, or to customize the buttons. Unfortunately, Microsoft hasn't seen fit to give developers more control over what seems to be a fairly simple api.
  • Simon D.
    Simon D. about 14 years
    Thanks for your explanation, at least now I know, when the X has an effect and when it does not. Now I still need to deal with it by either hiding the X or by styling it in such a way, that the user is not confused when he clicks it several times without any response.
  • Simon D.
    Simon D. about 13 years
    Marked as answer, because it at least answers WHY this is so, even if there is no nice solution provided how to change it.
  • ioWint
    ioWint about 13 years
    @Simon D: Thanks! ( i was replying in order, didnt notice its u who commented twice! :) )
  • reckface
    reckface over 5 years
    I just noticed this behaviour!