C # Why form.Close () does not close the form?

I have a button click event handler with the following pseudo code:

private void btnSave_Click(object sender, EventArgs e) { if(txt.Text.length == 0) this.Close(); else // Do something else // Some other code... } 

It's just some simple code, but the fact is that when the length of the text is zero, I want to close the form. But instead of closing the form, the code executes part // Another code. After the click event handler is fully executed, the form closes.

I know when I put return right after this.Close() , the form closes, but I would like to know WHY the form is not closed when you call this.Close() . Why is the rest of the event handler processing done?

+6
c # winforms
source share
6 answers

The rest of the event handler executes because you did not leave this method. It is so simple.

Calling this.Close() does not immediately โ€œdeleteโ€ the form (and the current event handler). The form will be collected later by the garbage collector if there are no more links to the form.

this.Close() is nothing more than a regular method call, and if the method does not throw an exception, you will remain in the context of your current method.

+11
source share

Close only hides the form; the form is still alive and will not receive another Load event if you show it again.

To actually remove it from memory, use Dispose ().

+4
source share

The answer is simple, since you are executing your current method, so this.Close () will be queued until you explicitly return, or your current exception method throws an exception.

+3
source share

Another possible solution is if you open a new Form and want to close the current one: if you use newForm.ShowDialog() instead of newForm.Show() , it does not close currentForm with currentForm.Close() until newForm will be closed.

+2
source share

If the form is not a modal form (opened with .ShowDialog ()), Form.Close () also provides the form. Therefore, you cannot resume it under any circumstances after that, despite what others could say. There is a form. Apparently for this behavior (hiding / showing the form).

The point here is that .Close () does not return from a section that is called for several reasons. For example, you can call SomeForm.Close () from another form or class, or any other.

Close () is just a method like any other. You must explicitly return from the method that calls Close (), if that is what you want.

0
source share

Calling MessageBox.Show (frmMain, "message", "header") adds the "TextDialog" form to the Application.OpenForms () application collection, along the frmMain most basic form. It stays after closing Messagebox.

When this happens, and you call the OK delegate to close the main form, calling frmMain.Close () will not work, the main form will not disappear, and the program will not end, as usual, after exiting OK, delegate. Only Application.Exit () will close all "TextDialog" message trash bins.

0
source share

All Articles