what is going on in Close()

  • Thread starter Thread starter Ashish
  • Start date Start date
A

Ashish

It might be basics for many but I never gave attention on this before.

Steps:

Add 2 forms (Form1/Form2) in application.
Create a object in Form1 for Form2.

Form2 f2 = new Form2(); //line 1
//f2.Show(); // line 2
f2.Close(); // line 3
f2.Show(); // works just fine // line 4

Give attn on line 2 as it is marked out. In line 4 it shows form. But when I
uncomment line 2 it gives error on line 4. I hope to get this behavior even
when line 2 is commented out.

1- why dispose is not called when I call Close when line 2 is commented?
2- what is happening in Close() when line 2 is commented?
3- why it is working when I create form object, call close on it, and then
show it?
4- what is happening when I do Show(), Close(), and then Show() ?

In my understanding object is in hidden state after creation. Show only
makes Visible property of form to true. Now I am sure there is something
more to it than just making the form visible.

Thanks a lot,

Ashish
 
hi ashish,

Basically Close and dispose are not same.
When u call close on an object, it just closes it.
For example the close call on a SQL connection closes the connection to the
SQL server.
This means that the same object can be and will be used again with a call to
the open connection command.

Dispose in this case disposes off the object itself, so that it cannot be
used again, u need to create a new object to use the object.

The case u have specified is similar to the webform,
U cannot close a form unless it is showing up.....

Hope it helped u get a basic idea about close and dispose,
Regds,
Kannan.V
 
Hi Kannan,

Actually I know what you told me. Condition is little different then just
calling Dispose() or Close() on the form. Dispose(bool) will be called from
Close() when we do a Show() before calling Close() (stack trace). I was
looking for something deeper in this problem. But thanks for the reply.
 
Hi Ashish,

Provided that the window handle has been created, Form.Close sends a
WM_CLOSE message to the window. It is up to the Form's message handling
routine to take it from there. If the window handle has not been created
yet, Form.Close does nothing. The window handle of a form is not created
when the form is created. The window handle does get created when the form
is made visible (there may be other things that would cause the window
handle to be created before the form is made visible).

Regards,
Daniel
 
Thanks Daniel, that is what I was looking for.


Daniel Pratt said:
Hi Ashish,

Provided that the window handle has been created, Form.Close sends a
WM_CLOSE message to the window. It is up to the Form's message handling
routine to take it from there. If the window handle has not been created
yet, Form.Close does nothing. The window handle of a form is not created
when the form is created. The window handle does get created when the form
is made visible (there may be other things that would cause the window
handle to be created before the form is made visible).

Regards,
Daniel
 
Back
Top