Help with some Beginner Code? - Small problem with entering text.
Hey VBForums. Hoping you guys can help me out with solving a small problem I have.
Code:
Private Sub GuessTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuessTextBox.TextChanged
If Not IsNumeric(GuessTextBox.Text) Then
GuessTextBox.Text = ""
MsgBox("Hey! Letters don't count. Only numbers")
End If
If IsNumeric(GuessTextBox.Text) > 1000 Then
GuessTextBox.Text = "1000"
End If
End Sub
I have this here, i'm making a simple 'Guess the Number' Kid's game.
If the user enters a non Integer number in the 'GuessTextBox' it will give them that message and erase the number in the box.
Every time the space becomes blank however the message prompts.
So if I type 125 then backspace it, it will give the message. How do I make it so this message only prompts when something non-numeric is placed in?
OR Is there a way I can make it so it simply doesn't allow anything but 1,2,3,4,5,6,7,8,9,0 in the box?
Thanks.
Re: Help with some Beginner Code? - Small problem with entering text.
vb.net Code:
Public Class Form1
Private Sub GuessTextBox_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles GuessTextBox.KeyDown
If e.KeyCode = Keys.Back Then Exit Sub
End Sub
Private Sub GuessTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuessTextBox.TextChanged
If Not IsNumeric(GuessTextBox.Text) Then
GuessTextBox.Text = ""
MsgBox("Hey! Letters don't count. Only numbers")
Exit Sub
End If
If IsNumeric(GuessTextBox.Text) > 1000 Then
GuessTextBox.Text = "1000"
End If
End Sub
End Class
Re: Help with some Beginner Code? - Small problem with entering text.
There are a few ways to do what you want. If you want to check it before you process the data then perform an 'IsNumeric()' check on the textbox data. If you wish to check it as you go, lookup the KeyPress event.
Code:
Private Sub Command1_Click()
If IsNumeric(Text1.text) = False then
MsgBox "Please Enter Numeric Data Only!"
Exit Sub
End If
End Sub
OR:
Private Sub Text1_KeyPress(KeyAscii as Integer)
' If less than 0 or > 9 then ignore
If KeyAscii < &H30 or KeyAscii > &H39 then
KeyAscii = 0
End If
End Sub
OR:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'Numeric only:
KeyAscii = Asc(UCase(Chr$(KeyAscii)))
Select Case KeyAscii
Case 0 To 32
Case 48 To 57
Case Else
KeyAscii = 0
End Select
End Sub
OR:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'Non-numeric only:
Select Case KeyAscii
Case 0 To 32
Case 65 To 90
Case Else
KeyAscii = 0
End Select
End Sub
Re: Help with some Beginner Code? - Small problem with entering text.
jemmer, the OP is using VB.NET not VB6! I have requested the thread moved.
Re: Help with some Beginner Code? - Small problem with entering text.
Yeh, realised that after posting and couldn't delete it...my bad :)
Re: Help with some Beginner Code? - Small problem with entering text.
Thread moved from the 'VB6 and Earlier' (aka VB 1998 and earlier) forum to the 'VB.Net' (VB2002 and later) forum