Hi All,
I have 3 text boxes that have user input to preform calculations. What I would like to do is if the user inputs improper values to share the error code messages with out having to define the messages in each one of the text boxes using the same code. I have looked into Public Share but am not sure that is the way to go, having a hard time figuring it out anyway.
Here is the code for one of the text boxes:

'text box for user input of the loan term and verifies the input values
    Private Sub txtLoanTerm_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLoanTerm.TextChanged

        If Me.txtLoanTerm.Text <> "" Then
            If IsNumeric(txtLoanTerm.Text) Then     'checks input for numeric value
                If txtLoanTerm.Text > 0 Then        'checks input to make sure number entered is more than zero
                Else                                'show error if conditions are not met
                    MessageBox.Show("Input Error Please Enter a Numeric value Greater Than Zero")
                    Me.txtLoanTerm.Text = ""
                    Me.txtLoanTerm.Focus()
                End If
            Else                                    'show error if conditions are not met
                MessageBox.Show("Input Error Please enter a valid numeric value")
                Me.txtLoanTerm.Text = ""
                Me.txtLoanTerm.Focus()
            End If
        End If

    End Sub

As it is now I use this code 3 times, just change the name for the text box.
Can anyone help me out on this please?
Thanks,
Ken

Recommended Answers

All 7 Replies

Use the same handler to handle all three text boxes:

Private Sub ValidateNumbers(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
  Dim ThisBox As TextBox
  
  ThisBox = CType(sender, TextBox)
  If ThisBox.Text <> "" Then
    If IsNumeric(ThisBox.Text) Then     'checks input for numeric value
      If CDbl(ThisBox.Text) <= 0 Then   'checks input to make sure number entered is more than zero
      	'show error if conditions are not met
        MessageBox.Show("Input Error Please Enter a Numeric value Greater Than Zero")
        ThisBox.Text = ""
        ThisBox.Focus()
      End If
    Else                                    
    	'show error if conditions are not met
      MessageBox.Show("Input Error Please enter a valid numeric value")
      ThisBox.Text = ""
      ThisBox.Focus()
    End If
  End If

End Sub

actually I don't understand what you mean...well but if you mean to show their different error messages in one messagbos only, maybe this would help.

Dim ErrMessage As String

If Lastname.Text ="" Then
    ErrMessage = ErrMessage & "Lastname must have a value." & NewLine
End If

If Firstname.Text ="" Then
    ErrMessage = ErrMessage & "Firstname must have a value." & NewLine
End If

MesagBox.Show(ErrMessage)

Teme64,
Thank you very much for your help, it works great. I just couldn't figure out how to accomplish it. I searched the web and so forth, no luck on that either.
Thanks Again,
Ken

jireh,
Thank you for your reply, I have have to play with that as well.
Ken

Teme64,
Sorry to bother you again but for the last couple of hours I have been trying to add in to check the text boxes for no input at all. I have tried adding it to your code so I don't have to repeat this one as well, but it won't work for me either. Here it is:

ElseIf ThisBox.Text.Trim = "" Then
                MessageBox.Show("Please press continue on the next screen" _
                & " and enter the missing information")
                ThisBox.Focus()      'Set focus to field 
            End If

At the present time I have it here and it does work but I have to repeat it 3 times again.

Dim strInterestRate As String   'string from interest rate text box
        strInterestRate = Me.txtInterestRate.Text
        'checks to make sure the Interest Rate box is not left empyt
        If Me.txtInterestRate.Text.Trim = "" Then
            MessageBox.Show("Please press continue on the next screen" _
            & " and enter the 'Interest Rate'")
            Me.txtInterestRate.Focus()  'Set focus to field 
        End If

Can you tell me what I am doing wrong?
Thanks,
Ken

Hi! If your question is solved, please mark the thread as solved. Thank you!

Teme64,
I had asked you, or anyone one more question in regards to this post. If you can try and reply to my last post I would really appreciate it. If not I will close this thread tomorrow.
Thanks,
Ken

Ok. I don't think you're doing anything wrong. Since the code handles all three boxes and threats all the same, there's no easy way to tell which is the "current" text box.

Let's modify:

Private Sub ValidateNumbers(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
  Dim ThisBox As TextBox
  Dim Msg As String
  ThisBox = CType(sender, TextBox)
  If ThisBox.Text <> "" Then
    If IsNumeric(ThisBox.Text) Then     'checks input for numeric value
      If CDbl(ThisBox.Text) > 0 Then        'checks input to make sure number entered is more than zero
      Else                                'show error if conditions are not met
        MessageBox.Show("Input Error Please Enter a Numeric value Greater Than Zero")
        ThisBox.Text = ""
        ThisBox.Focus()
      End If
    Else                                    'show error if conditions are not met
      MessageBox.Show("Input Error Please enter a valid numeric value")
      ThisBox.Text = ""
      ThisBox.Focus()
    End If
  Else ' HERE THE BOX IS EMPTY
    ' "Generic" solution
    MessageBox.Show("Please press continue on the next screen" _
      & " and enter the missing data")
    ThisBox.Focus()
    ' 2nd solution
    ' Since you want to identify exact box:
    Msg = ""
    If TextBox1.Text.Trim = "" Then
      Msg = "Interest rate"
    End If
    If TextBox2.Text.Trim = "" Then
      Msg = "Loan term"
    End If
    If TextBox3.Text.Trim = "" Then
      Msg = "This item"
    End If
    MessageBox.Show("Please press continue on the next screen" _
      & " and enter the '" & Msg & "'")
    ThisBox.Focus()
  End If
End Sub

Now there's a two ways to do it. You may prefer second solution since you want to give user the exact information of what is missing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.