Question Me.Close or Me.Dispose

capedech

Well-known member
Joined
Oct 29, 2008
Messages
62
Programming Experience
Beginner
What's the different between Me.Close and Me.Dispose for closing a Form ?
What I know, Me.Dispose is included in Syntax Me.Close. Am I right ?

My friend did this to close a Form :
Me.Close
Me.Dispose

Does it necessary ?
 
Have a look at Form.Close method in help. Notice these remarks:
help said:
The two conditions when a form is not disposed on Close is when
(1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and
(2) you have displayed the form using ShowDialog.
In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.
 
but what the difference between those?
Basically it means that if you display a form by calling Show then call Close will dispose it, while if you display the form by calling ShowDialog calling Close will not dispose it. That's basically so you can use the same form multiple times as a modal dialogue. If you only want to use a dialogue once then you should create it with a Using block, which will also dispose it:
VB.NET:
Using dlg as New DialogueForm
    If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
        'The user clicked OK.
    End If
End Using 'The form gets disposed here.
 
Back
Top