validation

cromie09

New member
Joined
Jan 21, 2010
Messages
2
Location
belfast
Programming Experience
Beginner
i have 5 textboxes with 5 labels representing candidates and a cast vote button i have set up the arrays and am trying to use the textboxes to vote giving preferences from 1 to 5 although i would like if possible to be able to have the choice of a 1,2,3 vote or a 1,2,3,4,5 vote that would be the voters choice i set the maximum character property to 1 so only 1 digit could be entered also didnt want letters or 0,6,7,8,9 so i entered

Private Sub TextBox1_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress, TextBox4.KeyPress, TextBox5.KeyPress
Select Case (e.KeyChar)
Case "1"
Case "2"
Case "3"
Case "4"
Case "5"
End Select
If Not Char.IsNumber(e.KeyChar) Then
e.Handled = True
End If

End Sub

but 6,7 etc can still be entered i am stuck how could i do this any help would be much appreciated
 
The select statement doesn't actually limit the value, you need to use a Default case to set the e.Handled = true. Also you can combine case statements into one line if their results will be the same.

Honestly, this would be better as an if statement.
(Not tested)
VB.NET:
If Char.IsNumber(e.KeyChar) Then
     Dim intTemp As Int32 = Convert.ToInt32(e.KeyChar)
     If intTemp > 5 OrElse intTemp < 1 Then
          e.Handled = True
     End If
Else
     e.Handled = True
End If

Even more so, use a numericupdown control instead of textboxes that will eliminate the need to wory about text or checking for it.
 
Set maximum characters to 1, as you have done. Then enter this code in the KeyPress event:

VB.NET:
If (e.KeyChar) < "1" Or e.KeyChar > "5" Then e.Handled = True
You may also need to validate that the same number has not been entered in more than one of the textboxes. In that case, you can use this code:

VB.NET:
	Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress, TextBox4.KeyPress, TextBox5.KeyPress
		Dim mykey As Char = e.KeyChar
		If mykey < "1" Or mykey > "5" Then
			e.Handled = True
		Else
			For Each ctl As Control In Controls
				If TypeOf ctl Is TextBox Then
					Dim txt As TextBox = CType(ctl, TextBox)
					If txt.Text = mykey Then
						MessageBox.Show("Duplicate choice not allowed")
						e.Handled = True
					End If
				End If
			Next
		End If
        End Sub


This may not work properly if you have more textboxes on your form and they have one of those numbers entered as text. In that case, you should add a Tag property to those textboxes that require a choice. For example, you could use K as a tag, and amend the code as follows:

VB.NET:
		Dim mykey As Char = e.KeyChar
		If mykey < "1" Or mykey > "5" Then
			e.Handled = True
		Else
			For Each ctl As Control In Controls
				If TypeOf ctl Is TextBox Then
					Dim txt As TextBox = CType(ctl, TextBox)
					If txt.Tag Is "K" And txt.Text = mykey Then
						MessageBox.Show("Duplicate choice not allowed")
						e.Handled = True
					End If
				End If
			Next
		End If
 
Last edited:
Back
Top