|
-
Aug 25th, 2014, 06:06 AM
#1
Thread Starter
Junior Member
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.
-
Aug 25th, 2014, 06:32 AM
#2
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
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Aug 25th, 2014, 06:35 AM
#3
Junior Member
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
-
Aug 25th, 2014, 06:37 AM
#4
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.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Aug 25th, 2014, 06:40 AM
#5
Junior Member
Re: Help with some Beginner Code? - Small problem with entering text.
Yeh, realised that after posting and couldn't delete it...my bad
-
Aug 25th, 2014, 06:53 AM
#6
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|