views:

139

answers:

2

I am working on learning Windows Forms with C# and have a bare bones application. I am trying to close it when the user selects File->Exit. I have an event handler attached to it and I have tried calling Application.Exit(), Application.ExitThread() and just closing the form. Nothing. It stays there. I'm not creating any other Threads of any sort either.

Ideas?

Thanks.

+3  A: 

Have you tried to put a breakpoint in the event handler to see if it is being hit?

If so, the application won't exit if the window messages aren't being delivered. One way to test this is to call Environment.Exit() which is more brutal about forcing a close. If this succeeds, you can then figure out why Application.Exit() isn't working.

codekaizen
auto completion in VS screwed me up. I was assigning the delegate to another similarly named menu item.
Casey
+1  A: 

Application.Exit isn't the normal way to close a GUI application. Use form.Close instead.

  private static void OnMenuClose_Click(object sender, System.EventArgs e)
  {
     Form dlg = ((Control) sender).FindForm();
     //dlg.DialogResult = DialogResult.OK;
     dlg.Close();
  }
John Knoeller
does that work if you have mutliple forms?
Casey
It does if you do it to the main form. If all other forms are children they will be destroyed automatically.
John Knoeller