Thursday, March 31, 2011

Why can I not close a form in C#?

I cannot close one of my forms programmatically. Can someone help me?

Here's the code:

    private void WriteCheck_Load(object sender, EventArgs e) {
        SelectBankAccountDialog sbad = new SelectBankAccountDialog();
        DialogResult result = sbad.ShowDialog();
        if (result == DialogResult.Cancel) {
            this.Close();
        } else {
            MessageBox.Show(result.ToString());
        }
        MessageBox.Show(sbad.bankaccountID.ToString());
    }
From stackoverflow
  • By calling Form.Close(), the form should close, but not until all waiting events have been processed. You also still have a chance to cancel the form closing in the FormClosing event.

    First, you'll probably want to return after your call to this.Close(). If it still doesn't close, step through your code and see what is happening. You may have to set and check a "forciblyClose" flag and return from any other processing methods before it'll actually close.

  • I would wrap all of that code in a try/catch and log the exception. By default, event handlers in Windows Forms will swallow exceptions, and I assume that is the case here.

    Malfist : What does wrapping everything in a try/catch block have to do with the question? Nothing is throwing an exception.
    Geoffrey Chetwood : @Malfist: Your tone conveys a severe lack of appreciation for people who are trying to help you.
    Malfist : No, I was just asking a question. I was not being hostile. However, you sir, are. I express my severe lack of appreciation for you Rich B.
  • The actual problem is that windows won't let a form close on it's load method. I have to show the dialog on construction, and then if the dialog result is cancel I have to throw an exception and catch the exception on form creation.

    This place talks more about it

    configurator : It's a little more subtle than that; the Close() call does close the form, but that is before the Show() or ShowDialog() has had the chance to show the form - so what happens is the form is first hidden, then shown (outside the Load event).
    Malfist : Ah, thank you for the clarification.
  • As configurator mentioned (in comments), the form must be shown before it can be closed, so, instead of the Load event, you should be doing this in the Shown event instead.

    If you don't want the form visible for the Dialog box, I guess you can wrap the event code in a Visible = false;

    In summary, the basic code would be

        private void WriteCheck_Shown(object sender, EventArgs e)
        {
            Visible = false;
            SelectBankAccountDialog sbad = new SelectBankAccountDialog();
            DialogResult result = sbad.ShowDialog();
            if (result == DialogResult.Cancel) {
                this.Close();
            } else {
                MessageBox.Show(result.ToString());
            }
            MessageBox.Show(sbad.bankaccountID.ToString());
            Visible = true;
        }
    
    Malfist : it can be visible, it just didn't have it as such.
    johnc : That's cool. It's a gotcha that caught be a year back, so happy to pass it on.

0 comments:

Post a Comment