tags:

views:

88

answers:

3

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...
}

This is just some simple code, but the point is, when the text length equals zero, I want to close the form. But instead of closing the form the code executes the part // Some other code. After the click event handler is completely executed, then the form is closed.

I know, when I place return right after this.Close() the form will close, but I'd like to know WHY the form isn't direclty closed when you call this.Close(). Why is the rest of the event handler executed?

+6  A: 

The rest of the event handler is executed because you did not leave the method. It is as simple as that.

Calling this.Close() does not immediately "delete" the form (and the current event handler). The form will be collected later on by the garbage collector if there are no more references to the form.

this.Close() is nothing than a regular method call, and unless the method throws an exception you will stay in the context of your current method.

0xA3
But intuitively I would except that when calling this.Close() the form will be closed, but this ain't the case. Thanks for the clarification.
Martijn
@Martijn: So the form stays open and does not close at all? Can you show what "some other code" is doing then? Or can you post a simple sample showing that behaviour?
0xA3
@0x3a: Incorrect. The form executes all the code in the event handler and then closes the form. You have already answered my question :) I was wondering why `Close()` doesn't destroy the form at the moment `Close()` is called.
Martijn
@martijn it will be closed when calling .Close() however closed is not destroyed. Considere this code: form.Close(); releaseResources(); where the last method calls takes a long time. Why would you want the user to see an unresponsive form when after all they will be able to do nothing more with it. In other words: There are times where you wish to close the form but still run some form code
Rune FS
@Rune FS: In that case, what is the purpose of a closed form? What kind of things aren't possible anymore? I can't imagine what a closed form means.
Martijn
@Martijn it means that the forms has been visually closed. It's no longer shown on screen and the _user_ can't interact with it.If you close a drawer do you expect it to be destroyed? or do you simply accept that you have to reopen it before you can interact with what you keep in the drawer? it's the same with closing a form. If you wish to interact with whats on the form you need to reopen it but just as the drawer is not destroyed so is also true for the form
Rune FS
@Rune FS: Thanks for the further explanation. Like the comparison with the drawer, it makes sense now! Thanks again
Martijn
+1  A: 

Answer is simple as you are executing your current method so this.Close() will be enqueued until either you explicitly returned or your current excuting method throws an exception.

saurabh
A: 

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

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

vulkanino
No, this is not correct. `Form.Close()` will dispose the form, i.e. it calls the `Dispose` method.
0xA3
Not exactly, if the form is an MDI client, and is invisible, Close won't Dispose. Or if the form is a dialog box (ShowDialog called), it won't Dispose either.
vulkanino
When I use `Dispose()` instead of `Close()` the rest of the code is still executed.
Martijn