2

I have a modal dialog, and need to hide the Close (X) button, but I cannot use ControlBox = false, because I need to keep the Minimize and Maximize buttons.

I need to hide just Close button, is there any way to do that?

Ehsan
  • 31,833
  • 6
  • 56
  • 65
Animesh Ghosh
  • 331
  • 8
  • 16
  • 28
  • 1
    First search result: [Windows Forms: How to hide Close (x) button?](http://stackoverflow.com/questions/7301825/windows-forms-how-to-hide-close-x-button) – Sergey Berezovskiy Jul 21 '13 at 13:33
  • 3
    Allowing a dialog box to be minimized makes no sense. The dialog will close. Allowing a dialog box to be maximized makes little sense, it only needs to be as big enough to present the dialog options. Preventing the user from closing a dialog makes little sense, especially when you don't make it look like a dialog. That's just a trap. Use sane UI practices and you'll get few complaints from Winforms and your user. – Hans Passant Jul 21 '13 at 13:57

3 Answers3

5

Although you can disable the close button as the answers here (and to the duplicate question) have suggested by adding the CS_NOCLOSE style to the form's window class, consider very seriously whether you really need to do that.

You still have to give the user some way of dismissing the modal dialog, presumably with buttons on the dialog itself. And since one of those buttons is probably "Cancel" or the equivalent, you should just make the Close (X) button do the same thing as "Cancel". (Handle the FormClosing or FormClosed event for your form if you want to customize the default behavior or do something special on closure.)

Note that the Windows UI guidelines for dialog boxes state explicitly that you should not disable the Close button because users expect to see it and it gives them a feeling of security that they can always safely "get out" of whatever popped up on the screen if they don't want it:

  • Dialog boxes always have a Close button. Modeless dialogs can also have a Minimize button. Resizable dialogs can have a Maximize button.
  • Don't disable the Close button. Having a Close button helps users stay in control by allowing them to close windows they don't want.
    • Exception: For progress dialogs, you may disable the Close button if the task must run to completion to achieve a valid state or prevent data loss.
  • The Close button on the title bar should have the same effect as the Cancel or Close button within the dialog box. Never give it the same effect as OK.
  • If the title bar caption and icon are already displayed in a prominent way near the top of the window, you can hide the title bar caption and icon to avoid redundancy. However, you still have to set a suitable title internally for use by Windows.

Even with progress dialogs, which Microsoft calls out as an "exception" to this general rule, it's often very desirable to make the operation cancellable.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

Hiding the close button can't be done. Though you can disble it by overriding the CreateParams property of the form. You can see the details of how to achieve this [here]1

private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
 get
 {
    CreateParams myCp = base.CreateParams;
    myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON ;
    return myCp;
 }

}

Ehsan
  • 31,833
  • 6
  • 56
  • 65
0

The best you can get is disabling the close button. Otherwise setting ControlBox = false will hide all buttons.

For disabling the close button, you can add this code to your form class:

protected override CreateParams CreateParams
{
    get
    {
       const int CP_NOCLOSE = 0x200;
       CreateParams myCp = base.CreateParams;
       myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE;
       return myCp;
    }
}

Explanation:

During construction and creation of the Form object, .NET would use the default creation parameters available in the base class CreateParams property. In fact, CreateParams property is available in Forms.Control class. In our form class (derived from System.Windows.Forms.Form), override this property and modify the creation flags. For disabling the Close button use 0x200 to modify the ClassStyle member of the CreateParams.

Source: Disabling Close Button on Forms

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185