Reputation: 55
I am new to vb net and I was trying to create a program that checks 2 text box inputs:
I have experience in MS Access vba where we can force input box to only accept numbers. But I found out that this was not possible in VB.net. So I did some research for integer cheking and came up with this:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.txtTeam1.Text = 0
Me.txtTeam2.Text = 0
End Sub
Private Sub txtTeam1_TextChanged(sender As Object, e As EventArgs) Handles txtTeam1.TextChanged
If checkType(Me.txtTeam1.Text) = True Then
changeBackColor()
Else
MessageBox.Show("Invlaid Entry", "Please Enter a number")
End If
End Sub
Private Sub txtTeam2_TextChanged(sender As Object, e As EventArgs) Handles txtTeam2.TextChanged
If checkType(Me.txtTeam2.Text) = True Then
changeBackColor()
Else
MessageBox.Show("Invlaid Entry", "Please Enter a number")
End If
End Sub
Function changeBackColor()
Try
If Convert.ToInt32(Me.txtTeam1.Text) < Convert.ToInt32(Me.txtTeam2.Text) Then
Me.txtTeam1.BackColor = Color.Gray
Me.txtTeam2.BackColor = Color.Green
ElseIf CInt(Me.txtTeam1.Text) > CInt(Me.txtTeam2.Text) Then
Me.txtTeam1.BackColor = Color.Green
Me.txtTeam2.BackColor = Color.Gray
Else
'Both are equal
Me.txtTeam1.BackColor = Color.Gray
Me.txtTeam2.BackColor = Color.Gray
End If
Catch ex As Exception
MessageBox.Show("Invlaid Entry", "Please Enter a number")
End Try
End Function
Function checkType(num As String) As Boolean
Try
Convert.ToInt32(num)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
However, as soon as the form loads I get the invalid entry message box even though the default value is 0. Even when I enter a valid number in either of the text box I get the invalid input message
Upvotes: 0
Views: 1328
Reputation: 25023
You can use the method Integer.TryParse
to check if a string can be parsed as an integer, and it doesn't throw an exception if it can't, it just returns True if it could parse the string into a number and puts the value in the second parameter, otherwise it returns False and sets the second parameter to 0 (actually it uses the default value of the type of the variable of the second parameter).
If you don't want the boxes to be changing colour as the user types, you can use the Validating event instead of the TextChanged event.
Furthermore, you can control when the event handler is applied by deleting the Handles ...
part and manually adding it with the command AddHandler
. That would prevent it from complaining at the moment you have set txtTeam1.Text = "0"
but before setting the other textbox. You can even use just one event handler to handle more than one event, like this:
Public Class Form1
Sub ChangeBackColors()
Dim team1 = 0
Dim team2 = 0
If Integer.TryParse(txtTeam1.Text, team1) AndAlso Integer.TryParse(txtTeam2.Text, team2) Then
If team1 < team2 Then
Me.txtTeam1.BackColor = Color.Gray
Me.txtTeam2.BackColor = Color.Green
ElseIf team1 > team2 Then
Me.txtTeam1.BackColor = Color.Green
Me.txtTeam2.BackColor = Color.Gray
Else
'Both are equal
Me.txtTeam1.BackColor = Color.Gray
Me.txtTeam2.BackColor = Color.Gray
End If
Else
MessageBox.Show("Invalid Entry", "Please enter a whole number.")
End If
End Sub
Private Sub txtTeam_Validating(sender As Object, e As EventArgs)
ChangeBackColors()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtTeam1.Text = "0"
txtTeam2.Text = "0"
AddHandler txtTeam1.Validating, AddressOf txtTeam_Validating
AddHandler txtTeam2.Validating, AddressOf txtTeam_Validating
End Sub
End Class
Upvotes: 1
Reputation: 36
You could do something like this.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If System.Text.RegularExpressions.Regex.IsMatch(TextBox1.Text, "[^0-9]") Then
MessageBox.Show("Please enter only numbers.")
TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.Length - 1)
End If
End Sub
If you want to know more you can take a look at this. How do I make a textbox that only accepts numbers?
Upvotes: 1