Halting Application to allow error corrections

 I'm using the ErrorHandler control in a sub to check the validity of entries in text boxes. It works just fine. When my sub returns false, meaning that there are errors, I would like to halt the execution of code while the user makes corrections and re-submits the form. This is in a desktop application. I've tried numerous things, but the code keeps executing after the error is returned.

IsValid()
If IsValid() = False Then

   ... This is where I'd like to stop execution of the trailing code and allow the user to make changes and resubmit the form.

End If

Thanks for the help.
A certified VB.Net Newbie
               


Answer this question

Halting Application to allow error corrections

  • n3bu1a

    I think I need to know a little more about the structure of your app . . . Is this happening on a button click   What sort of things do you want to happen if the data is valid   Tell me about your form and what you mean by "Submit."
  • S Kaliyan

    JacobMVP,

        Thanks for the help. I can't believe that I didn't see that. It must be time to go home for the day.
         Again, thanks.

    Mark F.

  • BobDaBuilder

    Okay, firstly, it's popping the message box twice because of this:

    IsValid() 
    If IsValid() = False Then 


    The first statement calls your IsValid function.  And then it gets called again because you are referencing it in your 'if' condition.

    I would simply remove the first call and you should be okay...

  • EJ Moritz

    You're saying it's a desktop application, and since you're on this forum, I guess you're writing a WinForms application. The term "submit" usually means ASP.NET, which might have confused some...

    I suggest you take a look at the ErrorProvider. It displays a red ! next to the text box if it's not valid. Here's some sample code:

    Private Sub TextBox1_Validation()
      If (... not valid ...) Throw New Exception("Invalid")
    End Sub

    Private Sub TextBox1_Validating(...) Handles TextBox1.Validating
      Try
        TextBox1_Validation()
      Catch ex As Exception
        ' Validation failed
        e.Cancel = True
        ' Set error provider
        ErrorProvider1.SetError(TextBox1, ex.Message)
      End Try
    End Sub

    Private Sub TextBox1_Validated(...) Handles TextBox1.Validated
      ' Text box is valid, clear error provider
      ErrorProvider1.SetError(TextBox1, "")
    End Sub


  • Tom Laird-McConnell

    My bad,
      
         I should have included code. Yes, I call the function on a click event. What I've now got is when one of the controls doesn't meet validation the messagebox pops up with the error. When you click on the OK button, the messagebox pops up again. When I click the OK the second time it resets the focus on the first textbox in the form like I want. I've been unable to figure out why the messagebox pops twice.

    Private Sub btnPnlLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPnlLoad.Click

            IsValid()
            If IsValid() = False Then
                txtPlateNum.Focus()
            Else

                      ....... Code to run if IsValid = True

             End if
    End Sub



    Private Function IsValid() As Boolean
            
            Dim status As Control

            For Each status In Controls
                If ep1.GetError(status) <> "" Then
                    MessageBox.Show("Please correct any errors and resubmit the form", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                    Return False
                End If
            Next
                  Return True
        End Function

         Again, thanks for the help.

    Mark F.

    New to VB.net, but having fun

  • BostonBakedBean

    Also, take a look at this show from MSDN TV:

    <a href="http://msdn.microsoft.com/msdntv/episode.aspx xml=episodes/en/20030703VSNETBH/manifest.xml">http://msdn.microsoft.com/msdntv/episode.aspx xml=episodes/en/20030703VSNETBH/manifest.xml</a>

    The validator shown here saves a lot of code compared to my previous post - couldn't find it before...

  • Halting Application to allow error corrections