Cancelling the form to unload in vb .net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lotus18
    Contributor
    • Nov 2007
    • 865

    Cancelling the form to unload in vb .net

    How to cancel the unload event of a form? I have this code in vb 6 and it is working:

    [CODE=vb]
    Private Sub Form_QueryUnloa d(Cancel As Integer, UnloadMode As Integer)
    If MsgBox("Are you sure you want to exit system?", vbYesNo, "Exit System") = vbYes Then
    Unload Me
    Else
    Cancel = 1
    End If
    End Sub
    [/CODE]

    I converted the codes to vb .net but I failed to make it:

    [CODE=vb.net]Private Sub frmMain_FormClo sing(ByVal sender As Object, ByVal e As System.Windows. Forms.FormClosi ngEventArgs) Handles Me.FormClosing
    If MessageBox.Show ("Are you sure you want to exit system?", "Exit System", MessageBoxButto ns.YesNo, MessageBoxIcon. Question) = Windows.Forms.D ialogResult.Yes Then
    Global.System.W indows.Forms.Ap plication.Exit( )
    Else
    Exit Sub
    End If
    End Sub[/CODE]

    Rey Sean
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    #2
    Originally posted by lotus18
    How to cancel the unload event of a form? I have this code in vb 6 and it is working:

    [CODE=vb]
    Private Sub Form_QueryUnloa d(Cancel As Integer, UnloadMode As Integer)
    If MsgBox("Are you sure you want to exit system?", vbYesNo, "Exit System") = vbYes Then
    Unload Me
    Else
    Cancel = 1
    End If
    End Sub
    [/CODE]

    I converted the codes to vb .net but I failed to make it:

    [CODE=vb.net]Private Sub frmMain_FormClo sing(ByVal sender As Object, ByVal e As System.Windows. Forms.FormClosi ngEventArgs) Handles Me.FormClosing
    If MessageBox.Show ("Are you sure you want to exit system?", "Exit System", MessageBoxButto ns.YesNo, MessageBoxIcon. Question) = Windows.Forms.D ialogResult.Yes Then
    Global.System.W indows.Forms.Ap plication.Exit( )
    Else
    Exit Sub
    End If
    End Sub[/CODE]

    Rey Sean
    Use the FormClosing eventargs

    [Code=vbnet]
    If MessageBox.Show ("Are you sure?", "Close?", MessageBoxButto ns.YesNo) = Windows.Forms.D ialogResult.No Then
    e.Cancel = True
    Else
    e.Cancel = False
    End If
    [/code]

    Comment

    • lotus18
      Contributor
      • Nov 2007
      • 865

      #3
      I just added this line.

      [CODE=vb .net]e.Cancel = True[/CODE]

      But now, the problem is, it showing the message dialog box twice when I clicked the Yes button. What may be the problem? What do you think?

      Rey Sean

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Do you have two event handlers assigned to the event?

        It should also be noted that you need to be looking at the e.CloseReason property too make sure it's not CloseReason.Win dowsShutDown or CloseReason.Tas kManagerClosing . You should not popup your messagebox if it's one those two.

        Comment

        Working...