Wednesday 23 November 2011

VB.Net Form Close vs Form Dispose

Two methods doing some identical jobs always create some confusion. What to use to 'unload' the form Form.Close or Form.Dispose

Form.Close should be the answer as going by Microsoft Form.Close disposes the form as well if the form Modeless .

One more advantage you get from this method is from the events that are associated with the form

You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler.

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Form is closing", "Form Close", MessageBoxButtons.YesNo, MessageBoxIcon.Hand) = Windows.Forms.DialogResult.No Then
e.Cancel = True
End If
End Sub

The above prevents the form being closed

When the form is opened as Modal form (using Showdialog) you can dispose the form explicitly

No comments:

Post a Comment