1

I know this is very simple question, but I am looking for neat and clean suggestion. My application is an MDI application. Created a base form which is being used in the whole applicaiton. There are couple of forms which we dont want user to close so to avoid human mistakes we have planned to not allow the user to close those forms.

I have tried to setting e.cancel = true in form closing event, but it seems its not working can somebody give me some suggestions please?

Edit:

Private void FrmTask_FormClosing(object sender, FormClosingEventArgs e)
        {         
                e.Cancel = true;
        }

The problem is that when I am using this code, none of the form in my MDI application are closing, even the main MDI Parent form.

Shax
  • 4,207
  • 10
  • 46
  • 62
  • You mean just prevent closing the window? Or even stopping the process? – Tudor Dec 19 '11 at 16:34
  • 1
    A code smaple would be helpful. e.Cancel = true; should do the trick. – Myles McDonnell Dec 19 '11 at 16:35
  • 3
    Please show us the code where you set `e.Cancel = true;` – Fischermaen Dec 19 '11 at 16:35
  • Setting `e.Cancel` should work. Describe what happens then. – H H Dec 19 '11 at 16:39
  • I'm all out of close votes, but this is a duplicate: [How can I prevent a user from closing my C# application?](http://stackoverflow.com/questions/4655810/how-can-i-prevent-a-user-from-closing-my-c-sharp-application). As others have mentioned, `e.Cancel` should work, but it's not a particularly robust solution. – Cody Gray - on strike Dec 19 '11 at 16:43
  • This is not a duplicate. This question is how to prevent an individual window in an MDI setup from closing, while the other one is to prevent an entire application from closing. I don't see that they are the same question, although of course they are related and perhaps they have the same answer. – Cyberherbalist Dec 19 '11 at 17:10
  • @Cyberherbalist: That's what duplicates are. They're questions with the same answer. – Cody Gray - on strike Dec 20 '11 at 00:13

3 Answers3

2

You could also not show the close button:

http://blogs.msdn.com/b/atosah/archive/2007/05/18/disable-close-x-button-in-winforms-using-c.aspx

Cody Gray provided a better link in the comments that also disallows the Alt-F4 close:

https://stackoverflow.com/a/4655948/366904

Community
  • 1
  • 1
Andrew Hanlon
  • 7,271
  • 4
  • 33
  • 53
  • 1
    But user can close the form with Alt+F4. – Fischermaen Dec 19 '11 at 16:38
  • 1
    Yes, this is the *wrong* way of disabling the close button. As Fischermaen mentions, the user will still be able to close the window using the keyboard. Instead, you should add the `CS_NOCLOSE` style to the window class, as explained [here](http://stackoverflow.com/a/4655948/366904). – Cody Gray - on strike Dec 19 '11 at 16:42
  • @CodyGray Nice find! I'll update the answer to include your link. If you post it as an answer I will upvote you to hopefully give you the accepted title. – Andrew Hanlon Dec 19 '11 at 17:45
  • 1
    @ach: I already posted it as an answer to the other question... About a year ago – Cody Gray - on strike Dec 19 '11 at 23:05
2

Presumably the issue here is you want the window to close when the app exists, but not if the user tries to close it manually.

To do this you will have to have a manual override, say a variable called allowShutdown defined, and have a method that can set this when the MdiParent is closing.

private void Form_Closing(object sender, EventArgs e)
{
  if( !allowShutdown) e.Cancel = true;
}

public void ForceShutdown()
{
  allowShutdown = true;
  Close();
}

And then in your parent form:

private void Form_Closing(object sender, EventArgs e)
{
  if( childForm != null ) childForm.ForceShutdown();
}

This assumes you are maintaining a reference to the child form in your parent form when you create it. Combine this with the ability to hide the close button mentioned in the other post and you should have a working solution.

samjudson
  • 56,243
  • 7
  • 59
  • 69
  • Yes, this would probably work, but it's a fairly ugly solution. You're preventing the user from closing the form without *telling* the user that they're not allowed to close the form. They will sit there clicking the "X" button in vain and nothing will happen. There's absolutely no clue that they're not allowed to close the window. The [solution](http://stackoverflow.com/a/4655948/366904) I linked to above is a much better option. It minimizes the amount of code you have to write in each form, and also makes it plainly obvious to the user that they're not allowed to close the window. – Cody Gray - on strike Dec 19 '11 at 16:49
  • ha ha ha, no there will be message displaying that do not close the dashboard as this is getting the live data, but to avoid human errors we are implementing this solution. – Shax Dec 19 '11 at 16:57
  • I said to implement this along with the visual changes in the other post. – samjudson Dec 19 '11 at 17:08
0

I suspect FormClosingEventArgs.CloseReason would give you enough information to determine whether to conditionally cancel the Close.

Joe
  • 122,218
  • 32
  • 205
  • 338