Kredns
Kredns

Reputation: 37211

Disabling the exit button on a windows form?

Is there a way to disable the exit button on a windows form without having to import the some external .dll's? I disable the exit button by importing dll's using the following code but I don't like it. Is there a simpler (built-in) way?

    public Form1()
    {
        InitializeComponent();
        hMenu = GetSystemMenu(this.Handle, false);
    }

    private const uint SC_CLOSE = 0xf060;
    private const uint MF_GRAYED = 0x01;
    private IntPtr hMenu;

    [DllImport("user32.dll")]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

    [DllImport("user32.dll")]
    private static extern int EnableMenuItem(IntPtr hMenu, uint wIDEnableItem, uint wEnable);

    // handle the form's Paint and Resize events 

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        EnableMenuItem(hMenu, SC_CLOSE, MF_GRAYED);
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        EnableMenuItem(hMenu, SC_CLOSE, MF_GRAYED);
    }

Upvotes: 4

Views: 26271

Answers (7)

Baz Guvenkaya
Baz Guvenkaya

Reputation: 1562

Disabling the button is possible without importing dlls. ControlBox = false causes minimise and maximise buttons and the border to disappear as well and it does not disable the close 'X' button - it hides. This solution disables it:

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;
    }
}

Source: https://www.codeproject.com/kb/cs/disableclose.aspx

Upvotes: 0

Saran N V
Saran N V

Reputation: 61

To disable the close button on the form just write the below code on the form closing event.

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = true;
    }

Upvotes: 6

Bevan
Bevan

Reputation: 44307

You can't easily disable the exit button (the one in the top right that closes the form).

You can, however, hide the button completely, by setting the ControlBox property to false.

ControlBox can be turned on and off at any time, so you can use this if you want to dynamically allow closing at some times, and not at others.

Alternatively, you can handle the FormClosing event and cancel the close if you choose.

Here's a demo.

Create a new Windows Forms project.

Drop a CheckBox control on the form, with Text "ControlBox". Hook up its Click event to this:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    ControlBox = checkBox1.Checked;
}

Then, drop a second CheckBox control on the form with Text "Cancel Close". Hook up the FormClosing event of the form to this:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = checkBox2.Checked;
}

Run the application and start playing around with the checkboxes. You'll soon see how things work.

Upvotes: 5

Jim H.
Jim H.

Reputation: 5579

A little poking around found this handy helper class:

Disable Close Button and Prevent Form Being Moved (C# version)

It actually does more than what you're looking for, but essentially does it very nearly the same way you do in your sample code. The helper class hooks into the load/resize events for you so you don't have to remember to do it yourself.

Upvotes: 4

Mike Bannister
Mike Bannister

Reputation: 53

Using visual studio select the form go to the properties and set the ControlBox property to false or try this.ControlBox = false; or frmMainForm.ControlBox = false;

Upvotes: 1

Judah Gabriel Himango
Judah Gabriel Himango

Reputation: 60021

Yes.

Setting the form.ControlBox = false will hide the close button. Although, it will also hide the minimize and maximize button.

You can also set form.FormBorderStyle = FormBorderStyle.None, which will hide the whole title bar.

If you want to show the X button but just stop the form from closing, override OnClosing and set the e.Cancel property to true.

Upvotes: 3

Ovidiu Pacurar
Ovidiu Pacurar

Reputation: 8209

Catch the FormClosing event and cancel it in the arguments.

Upvotes: 2

Related Questions