Does pressing the cancel icon on a form dispose it ?

sudhanshu22

Member
Joined
Nov 8, 2006
Messages
20
Programming Experience
Beginner
Hi all,
In a Vb.net form, we have cancel icon on right side top of the form. When we press it, we can close the form. when we click this icon, does the form get disposed ( meaning its memory is ready to be freed by garbage collector).
Or it still remains in memory and we have to specifically write like Me.dispose()
if we want to free its memory.
Another thing, if we want to associate cancel event with some works, how do we do it ? Specifically, there is a form1_closing event. Does this event do that task ?
 
Calling Dispose doesn't free any memory. It releases unmanaged resources. In the case of a form that means its window handle and those of any controls it contains. The memory it occupied is still occupied until the garbage collector decides it's required and reclaims it.

If you display a form by calling its Show method then closing it will dispose it. Pressing the Close button on the title bar is one way to do that. Calling its Close method is another. In that case the FormClosing and FormClosed events will be raised and you can perform any operations there that you deem necessary. Note that the Closing and Closed events are obsolete in .NET 2.0.

If you display a form by calling its ShowDialog method then closing it will NOT dispose it. In this case closing the form will just set its DialogResult property to Cancel if it was None and then set the Visible property to False, thus effectively hiding the form. Any form displayed using ShowDialog requires explicit disposal, i.e. you must call its Dispose method yourself when you're done with it.
 
Having said that explicit disposal is required, that's not completely true. VB 2005 allows implicit object disposal via the Using statement. You should always employ a Using block when creating any disposable objects that will only be required within the current block.
 
Back
Top