Windows Phone Developers

Friday, June 8, 2007

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


RSS Feeds Submission Directory



alt="Webfeed (RSS/ATOM/RDF) registered at http://www.feeds4all.com" border="0">
Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

4 comments:

  1. What about Windows shutdwon with your example? Is it ok?

    ReplyDelete
  2. In the FormClosing event handler:

    If e.CloseReason = CloseReason.WindowsShutDown Then
    code
    End If

    ReplyDelete
  3. i want to know ,when i run the vb windows forms in amalgam ..how can i close those forms automatically ..... .....(eg Public Class Form5


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim frm5 As New Form6
    Form6.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim frm5 As New Form7
    Form7.Show()
    Form6.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim frm5 As New Form8
    Form8.Show()
    Form7.Close()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Dim frm5 As New Form9
    Form9.Show()
    Form8.Close()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Dim frm5 As New Form10
    Form10.Show()
    Form9.Close()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    Dim frm5 As New Form11
    Form11.Show()
    Form10.Close()
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    Dim frm5 As New Form12
    Form12.Show()
    Form11.Close()
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
    Dim frm5 As New Form13
    Form13.Show()
    Form12.Close()
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
    Dim frm As New Form14
    Form14.Show()
    Form13.Close()
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
    Dim frm5 As New Form15
    Form15.Show()
    Form14.Close()

    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
    Dim frm5 As New Form16
    Form16.Show()
    Form15.Close()


    End Sub
    End Class)when i click form 2 o 3 i want to hide other forms...code plz.....

    ReplyDelete
  4. Close doesn't work it leaves it open I am trying to use the Form Unload event to do this. I can't believe how difficult microsoft has made this they should have stayed with VB6

    ReplyDelete