Results 1 to 6 of 6

Thread: Help with some Beginner Code? - Small problem with entering text.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    21

    Unhappy 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.

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Help with some Beginner Code? - Small problem with entering text.

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub GuessTextBox_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles GuessTextBox.KeyDown
    4.         If e.KeyCode = Keys.Back Then Exit Sub
    5.     End Sub
    6.  
    7.     Private Sub GuessTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuessTextBox.TextChanged
    8.         If Not IsNumeric(GuessTextBox.Text) Then
    9.             GuessTextBox.Text = ""
    10.             MsgBox("Hey! Letters don't count. Only numbers")
    11.             Exit Sub
    12.         End If
    13.         If IsNumeric(GuessTextBox.Text) > 1000 Then
    14.             GuessTextBox.Text = "1000"
    15.         End If
    16.     End Sub
    17. 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

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Location
    Wales, UK
    Posts
    24

    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

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Location
    Wales, UK
    Posts
    24

    Re: Help with some Beginner Code? - Small problem with entering text.

    Yeh, realised that after posting and couldn't delete it...my bad

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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
  •  



Click Here to Expand Forum to Full Width