WinForms: Disabling the close button

What is desired

You do not want the user to be able to click on the Close button (the little “x” in the upper right).

Method #1: The ControlBox property

The WinForm ControlBox property is a good place to start, but it shows or hides the entire control box. That means it either shows or hides all three buttons. That isn’t useful because one maywant the minimize and maximize buttons to work or because one may want those buttons to appear for visual consistency.

Method #2: Disabling the Close button directly

This thread on microsoft.public.dotnet.framework has a solution

The result:
– the control box is still visible (min, max, close)
– the close button will appear disabled
– clicking on the close button will not do anything

[Update on 2005-11-25, a clarification in reply to a comment]
This technique does correctly show the close button as disabled. It does not merely disable the *clickability*, it also correctly alters the appearance of the close button.

Comments (0)

Skip to main content