Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / Visual Basic
Alternative
Tip/Trick

Disable the Close box on a form

Rate me:
Please Sign up or sign in to vote.
3.56/5 (6 votes)
1 Feb 2010CPOL 7K   1   1
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.

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI Agree with the Griff here much easier and less confusing f... Pin
charles henington27-Jun-11 12:00
charles henington27-Jun-11 12:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.