Error Handling

Private mobjCOMException As System.Runtime.InteropServices.COMException 
Private mobjException As Exception

Form Load

Private Sub frmMain_Load(ByVal sender As System.Object, _ 
                         ByVal e As System.EventArgs) _
                         Handles MyBase.Load
   Try
      If clsError.ErrorFlag() = True Then Exit Sub



   Catch objCOMException As System.Runtime.InteropServices.COMException
      mobjCOMException = objCOMException
   Catch objException As Exception
      mobjException = objException

   Finally
      If gbDEBUG = True Or _
         ((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

         Call clsError.Handle("frmMain_Load", msCLASSNAME, _
              "load the form successfully.", _
              mobjCOMException, mobjException)
      End If
   End Try
End Sub

Controls

Private Sub Control_Click(ByVal sender As System.Object, _ 
                          ByVal e As System.EventArgs) _
                          Handles btnOK.Click
   Try
      If gbfrmMainForm_FireEvents = False Then Exit Sub
      If clsError.ErrorFlag() = True Then Exit Sub


   Catch objCOMException As System.Runtime.InteropServices.COMException
      mobjCOMException = objCOMException
   Catch objException As Exception
      mobjException = objException

   Finally
      If gbDEBUG = True Or _
         ((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

         Call clsError.Handle("Control_Click", msCLASSNAME, _
              "respond to the control successfully.", _
              mobjCOMException, mobjException)
      End If
   End Try
End Sub

OK Button

Private Sub btnOK_Click(ByVal sender As System.Object, _ 
                        ByVal e As System.EventArgs) _
                        Handles btnOK.Click
   Try
      If clsError.ErrorFlag() = True Then Exit Sub



   Catch objCOMException As System.Runtime.InteropServices.COMException
      mobjCOMException = objCOMException
   Catch objException As Exception
      mobjException = objException

   Finally
      If gbDEBUG = True Or _
         ((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

         Call clsError.Handle("btnOK_Click", msCLASSNAME, _
              "respond to the control successfully.", _
              mobjCOMException, mobjException)
      End If
   End Try
End Sub

Cancel Button

You must always be able to close the form regardless of whether an error has occurred.

Private Sub btnCancel_Click(ByVal sender As System.Object, _ 
                            ByVal e As System.EventArgs) _
                            Handles btnCancel.Click
   Try
'this is the correct method form forms that have been displayed using Frm.ShowDialog (i.e. modal)
'this implicitly calls the Me.Close method so you do not need both
      Me.DialogResult = Windows.Forms.DialogResult.Cancel

'this is the correct method form forms that have been displayed using Frm.Show (i.e. modeless)
      Me.Close

   Catch objCOMException As System.Runtime.InteropServices.COMException
      mobjCOMException = objCOMException
   Catch objException As System.Exception
      mobjException = objException

   Finally
      If gbDEBUG = True Or _
         ((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

         Call clsError.Handle("btnCancel_Click", msCLASSNAME, _
              "respond to the control successfully.", _
              mobjCOMException, mobjException)
      End If
   End Try
End Sub

Form Close

This is only needed if you are saving any settings even when the dialog box is closed using the X in the corner, such as saving the position of the form.

Private Sub frmMain_Close() 

   Try


   Catch objCOMException As System.Runtime.InteropServices.COMException
      mobjCOMException = objCOMException
   Catch objException As System.Exception
      mobjException = objException

   Finally
      If gbDEBUG = True Or _
         ((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

      Call clsError.Handle("frmName_Main", msCLASSNAME, _
           "unload the form successfully.", _
           mobjCOMException, mobjException)
      End If
   End Try
End Sub

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext