65.9K
CodeProject is changing. Read more.
Home

Disable the Close box on a form

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.56/5 (6 votes)

Feb 1, 2010

CPOL
viewsIcon

7321

I could be missing the point here, but I believe you could also override the OnClosing event.protected override void OnClosing(CancelEventArgs e){ // allow default behavior.. base.OnClosing(e); // test for whatever... // on fail, e.Cancel = true; // set some...

I could be missing the point here, but I believe you could also override the OnClosing event.
protected override void OnClosing(CancelEventArgs e)
{
    // allow default behavior..
    base.OnClosing(e);

    // test for whatever...
    // on fail, e.Cancel = true;
    // set some message, etc.

}
This method allows you to disable the action of closing, but leaves the "Close" button enabled - the user can then click it - but nothing happens. This may be confusing, unless you provide a MessageBox or similar to remind the user why he cannot close.