Try/Catch block within a Tab Page validating event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chadwick

    #1

    Try/Catch block within a Tab Page validating event

    Hi everyone -
    I have a form that contains a TabControl object with 2 tab pages,
    TabPage1 and TabPage2. TabPage1 contains two text boxes and a cancel
    button, which closes the form.
    I would like to validate the data contained in two text boxes on
    TabPage1 before the user moves to TabPage2, using TabPage1's
    validating event. My code looks like this:

    Public Class TabPageValidati onTester

    Private Sub ValidateTextBox es()
    'Otherwise complex processing simplified here.
    If TextBox1.Text = TextBox2.Text Then
    Dim ex As New System.Exceptio n("Strings in _
    TextBox1 and TextBox2 must be different.")
    Throw ex
    End If
    End Sub

    Private Sub TabPage1_Valida ting(ByVal sender As System.Object, _
    ByVal e As
    System.Componen tModel.CancelEv entArgs) _
    Handles TabPage1.Valida ting
    Try
    ValidateTextBox es()
    Catch ex As Exception
    MessageBox.Show ("Error. " & ex.Message)
    e.Cancel = True
    End Try
    End Sub

    Private Sub btnCancel_Click (ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles btnCancel.Click
    Me.Close()
    End Sub
    End Class
  • chadwick

    #2
    Re: Try/Catch block within a Tab Page validating event

    On Apr 16, 2:14 pm, chadwick <chad.mi...@gma il.comwrote:
    Hi everyone -
    I have a form that contains a TabControl object with 2 tab pages,
    TabPage1 and TabPage2. TabPage1 contains two text boxes and a cancel
    button, which closes the form.
    I would like to validate the data contained in two text boxes on
    TabPage1 before the user moves to TabPage2, using TabPage1's
    validating event. My code looks like this:
    >
    Public Class TabPageValidati onTester
    >
    Private Sub ValidateTextBox es()
    'Otherwise complex processing simplified here.
    If TextBox1.Text = TextBox2.Text Then
    Dim ex As New System.Exceptio n("Strings in _
    TextBox1 and TextBox2 must be different.")
    Throw ex
    End If
    End Sub
    >
    Private Sub TabPage1_Valida ting(ByVal sender As System.Object, _
    ByVal e As
    System.Componen tModel.CancelEv entArgs) _
    Handles TabPage1.Valida ting
    Try
    ValidateTextBox es()
    Catch ex As Exception
    MessageBox.Show ("Error. " & ex.Message)
    e.Cancel = True
    End Try
    End Sub
    >
    Private Sub btnCancel_Click (ByVal sender As System.Object, ByVal e
    As System.EventArg s) Handles btnCancel.Click
    Me.Close()
    End Sub
    End Class
    Sorry - accidentally posted before finishing my question. My problem
    is this: if the user enters text in the boxes, the event successfully
    cancels and the focus remains on TabPage1, but the Cancel button on
    TabPage1 loses its functionality! That is, when the Cancel button is
    clicked, the btnCancel.Click ed event is no longer raised. I have no
    idea what cause this. I would like my user to be able to hit cancel
    and leave the form after attempting to enter information in the text
    box and failing. Any help out there would be greatly appreciated.

    Comment

    • Steve Gerrard

      #3
      Re: Try/Catch block within a Tab Page validating event

      chadwick wrote:
      > Private Sub TabPage1_Valida ting(ByVal sender As System.Object, _
      > ByVal e As
      >System.Compone ntModel.CancelE ventArgs) _
      >Handles TabPage1.Valida ting
      > Try
      > ValidateTextBox es()
      > Catch ex As Exception
      > MessageBox.Show ("Error. " & ex.Message)
      > e.Cancel = True
      > End Try
      > End Sub
      >>
      >
      Sorry - accidentally posted before finishing my question. My problem
      is this: if the user enters text in the boxes, the event successfully
      cancels and the focus remains on TabPage1, but the Cancel button on
      TabPage1 loses its functionality! That is, when the Cancel button is
      clicked, the btnCancel.Click ed event is no longer raised. I have no
      idea what cause this. I would like my user to be able to hit cancel
      and leave the form after attempting to enter information in the text
      box and failing. Any help out there would be greatly appreciated.
      The issue is that if you cancel in the validating event of TabPage1, the focus
      can't move to the Cancel button. A cancelled validation is basically modal, and
      must be corrected before you can do anything.

      Fortunately, the fix is easy. Set btnCancel.Cause sValidation = False. :)







      Comment

      • Cor Ligthert[MVP]

        #4
        Re: Try/Catch block within a Tab Page validating event

        Chadwick,

        AFAIK is there is no tabpage closing event only a tabpage changed event, so
        this will in my idea be inpossible, however don't be afraid, the user won't
        see it as you set the selected page back to the one it was.

        Cor

        "chadwick" <chad.mills@gma il.comschreef in bericht
        news:22e4663d-2677-47f7-83df-c7f69ad870b3@q1 0g2000prf.googl egroups.com...
        On Apr 16, 2:14 pm, chadwick <chad.mi...@gma il.comwrote:
        >Hi everyone -
        > I have a form that contains a TabControl object with 2 tab pages,
        >TabPage1 and TabPage2. TabPage1 contains two text boxes and a cancel
        >button, which closes the form.
        > I would like to validate the data contained in two text boxes on
        >TabPage1 before the user moves to TabPage2, using TabPage1's
        >validating event. My code looks like this:
        >>
        >Public Class TabPageValidati onTester
        >>
        > Private Sub ValidateTextBox es()
        > 'Otherwise complex processing simplified here.
        > If TextBox1.Text = TextBox2.Text Then
        > Dim ex As New System.Exceptio n("Strings in _
        > TextBox1 and TextBox2 must be different.")
        > Throw ex
        > End If
        > End Sub
        >>
        > Private Sub TabPage1_Valida ting(ByVal sender As System.Object, _
        > ByVal e As
        >System.Compone ntModel.CancelE ventArgs) _
        >Handles TabPage1.Valida ting
        > Try
        > ValidateTextBox es()
        > Catch ex As Exception
        > MessageBox.Show ("Error. " & ex.Message)
        > e.Cancel = True
        > End Try
        > End Sub
        >>
        > Private Sub btnCancel_Click (ByVal sender As System.Object, ByVal e
        >As System.EventArg s) Handles btnCancel.Click
        > Me.Close()
        > End Sub
        >End Class
        >
        Sorry - accidentally posted before finishing my question. My problem
        is this: if the user enters text in the boxes, the event successfully
        cancels and the focus remains on TabPage1, but the Cancel button on
        TabPage1 loses its functionality! That is, when the Cancel button is
        clicked, the btnCancel.Click ed event is no longer raised. I have no
        idea what cause this. I would like my user to be able to hit cancel
        and leave the form after attempting to enter information in the text
        box and failing. Any help out there would be greatly appreciated.

        Comment

        • chadwick

          #5
          Re: Try/Catch block within a Tab Page validating event

          Hi Steve - thanks for your reply. I have tried setting
          btnCancel.Cause sValidation = False, but it didn't seem to work.

          An interesting note: the code works perfectly if I comment out the
          MessageBox.Show command:

          Public Class TabPageValidati onTester

          Private Sub ValidateTextBox es()
          'Otherwise complex processing simplified here.
          If TextBox1.Text = TextBox2.Text Then
          Dim ex As New System.Exceptio n("Strings in " _
          & "TextBox1 and TextBox2 must be different.")
          Throw ex
          End If
          End Sub

          Private Sub TabPage1_Valida ting(ByVal sender As System.Object, _
          ByVal e As
          System.Componen tModel.CancelEv entArgs) _
          Handles TabPage1.Valida ting
          Try
          ValidateTextBox es()
          Catch ex As Exception
          'MessageBox.Sho w("Error. " & ex.Message)
          e.Cancel = True
          End Try
          End Sub

          Private Sub btnCancel_Click (ByVal sender As System.Object, ByVal e
          _
          As System.EventArg s) Handles
          btnCancel.Click
          Me.Close()
          End Sub
          End Class

          I think my issue is related to the fact that the MessageBox.Show
          command takes focus away from the controls on TabPage1, but I can’t
          figure out the details. I could use an ErrorProvider, which would
          probably work, but ErrorProviders aren't stark enough for my purposes
          and I'd prefer to us a MessageBox. Any further ideas out there?

          Comment

          • Steve Gerrard

            #6
            Re: Try/Catch block within a Tab Page validating event

            chadwick wrote:
            Hi Steve - thanks for your reply. I have tried setting
            btnCancel.Cause sValidation = False, but it didn't seem to work.
            >
            Odd. I copy/pasted your code, and it worked fine for me.


            Comment

            Working...