Monday, September 18, 2006

Removing the close box from a c# form

I have a child of a MDI container and I don't want the user to be able to close it, or maximize, or minimize it. But I do want the user to be able to resize it and drag it around and will. So I want to get rid of the close, minimize and maximize buttons but retain the blue strip.

If you write some code like so:


this.ControlBox = false;
this.MaximizeBox = false;
this.MinimizeBox = false;


Where "this" is some form, you may find yourself with a control lacking a blue strip to move the form around! The solution to this problem is to enter some text in the text field.


this.Text = "My useful form";
this.ControlBox = false;
this.MaximizeBox = false;
this.MinimizeBox = false;


And that seems to be how it's done. Of course there are other ways override the message handler or using PInvoke (yes I tried Google before working this out for myself) but you really don't need to.

2 comments:

Bytes said...

You can always add a delegate to listen for the FormClosing event.

When you receive it -that is, the user tried to close the form-, you could then cancel it, like in:

private void FormClosingEvent(object sender, FormClosingEventArgs e)
{
e.Cancel = !buttonExit.Enabled;
}

In this case, it only allows closing if a specific exit button is enabled; obviously you can set here your own conditions

Henrikas said...

best way :)

For closing I use oposite: this.ControlBox = true;