• HEXUS
  • HEXUS.tv
  • channel
  • gaming
  • lifestyle
  • trust
  • community
  • ESReality
  • HEXUS.community discussion forumsVisit Corsair.com

    Welcome to the HEXUS.community discussion forums forums.

    You are currently viewing our boards as a guest which gives you limited access to view most discussions and other features. By joining our free community you will have access to post topics, respond to polls and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!
    Go Back   HEXUS.community discussion forums > HEXUS.help - buying advice & technical queries > Operating systems & applications > Software and web development

    Software and web development Databases, graphics, programming, scripting and web development.

    Reply
     
    LinkBack Thread Tools
    Old 25-01-2004, 04:20 PM   #1 (permalink)
    Flak Monkey!
     
    Dorza's Avatar
     
    Join Date: Jul 2003
    Location: UK - South Wales
    Posts: 1,762
    Thanks: 36
    Thanked 17 Times in 15 Posts
    Dorza's system
    Validating text entered into a VB text box

    Ok im makeing a program which asks a question and the user has to enter an answer into a txt box. When they do this an OK button appears and when that is clicked it tells them if their question is right or wrong. The problem i have is answers can only be numbers and not letters. How do i get the "ok" button to validate the answer in the text box. If a number is entered the program just gives feed back to the answer . If any thing other than a number is entered it displays a message saying "only numbers can be entered" for example. How do i do this? thanks for anyhelp.

    Edit: I know how to do all the other things mentioned. Its just that validation thing i cant work out
    Dorza is offline   Reply With Quote
    Old 25-01-2004, 06:17 PM   #2 (permalink)
    The stug
     
    Shad's Avatar
     
    Join Date: Jul 2003
    Location: In front
    Posts: 2,636
    Thanks: 14
    Thanked 18 Times in 15 Posts
    Shad's system
    If not isNumeric(val) then...

    My name is Simon

    Shad is offline   Reply With Quote
    Old 25-01-2004, 06:25 PM   #3 (permalink)
    Member
     
    Join Date: Aug 2003
    Location: Wimbledon
    Posts: 141
    Thanks: 0
    Thanked 0 Times in 0 Posts
    In this situation it is better to prevent the user from entering letters in the text box rather than wait til they press the OK button.

    What you need to do is handle the KeyPress event and check which key was pressed. If you set the keycode to 0 they the KeyPress is effectively cancelled.

    In the code below all key presses are cancelled apart from 0 to 9...

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case Asc("0") To Asc("9")
                ' Do nothing
            Case Else
                KeyAscii = 0
        End Select
    End Sub
    
    Mart is offline   Reply With Quote
    Old 25-01-2004, 08:23 PM   #4 (permalink)
    I live in a giant bucket.
     
    eldren's Avatar
     
    Join Date: Jul 2003
    Location: South Africa
    Posts: 617
    Thanks: 0
    Thanked 0 Times in 0 Posts
    Note that Mart's method will not work if the user pastes text using the right-click pop-up menu. Note also that Mart's method prevents the user from pressing backspace (ASCII char 8, I think).

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case Asc("0") To Asc("9"), 8 'Allow backspace
                ' Do nothing
            Case Else
                KeyAscii = 0
        End Select
    End Sub
    
    Private Sub Text1_Change()
        Dim i as Long, sTemp as String
    
        For i = 1 To Len(Text1)
            If Instr(1, "0123456789", Mid$(Text1, i, 1)) > 0 Then sTemp = sTemp & Mid$(Text1, i, 1)
        Next
    
        Text1 = sTemp
    End Sub
    
    That ought to do it. Mind you, I don't currently have VB installed, so the counting might be off, in which case, change "For i = 1" to "For i = 0". Hopefully, though, it's right. Stupid 1-based counting.

    eldren is offline   Reply With Quote
    Old 25-01-2004, 08:30 PM   #5 (permalink)
    Member
     
    Join Date: Aug 2003
    Location: Wimbledon
    Posts: 141
    Thanks: 0
    Thanked 0 Times in 0 Posts
    Note Mart is a VC++ programmer not a VB programmer
    Mart is offline   Reply With Quote
    Old 25-01-2004, 08:38 PM   #6 (permalink)
    I live in a giant bucket.
     
    eldren's Avatar
     
    Join Date: Jul 2003
    Location: South Africa
    Posts: 617
    Thanks: 0
    Thanked 0 Times in 0 Posts
    Fairly nice code you got there for a non-VB programmer.

    Myself, I'm currently employed coding VB. Though I know several other languages, a smidgeon of VC++ too.

    eldren is offline   Reply With Quote
    Old 29-01-2004, 09:12 PM   #7 (permalink)
    Flak Monkey!
     
    Dorza's Avatar
     
    Join Date: Jul 2003
    Location: UK - South Wales
    Posts: 1,762
    Thanks: 36
    Thanked 17 Times in 15 Posts
    Dorza's system
    Nice one. thanks everyone
    Dorza is offline   Reply With Quote
    Old 18-03-2004, 07:45 PM   #8 (permalink)
    Flak Monkey!
     
    Dorza's Avatar
     
    Join Date: Jul 2003
    Location: UK - South Wales
    Posts: 1,762
    Thanks: 36
    Thanked 17 Times in 15 Posts
    Dorza's system
    Re opening an old post

    as i said above thanks for all your help. the code eldren provided worked best for my situation however i now need prevent the user from entering zero first then enter numbers after it e.g. 0333. However they are allowed to enter zeros after a first number is entered whcih is not zero e.g. 10350. How is this done? took 0 away from the list of allowed character but realized then that no 0 could be entered which is where i got stuck. lol any ideas?
    Dorza is offline   Reply With Quote
    Old 19-03-2004, 07:38 PM   #9 (permalink)
    I live in a giant bucket.
     
    eldren's Avatar
     
    Join Date: Jul 2003
    Location: South Africa
    Posts: 617
    Thanks: 0
    Thanked 0 Times in 0 Posts
    First, declare a module-level variable that will tell us whether there is a number in the textbox or not. For example: Private blnAllowZero As Boolean

    Now, in the Keypress sub, change 'Asc("0") To Asc("9")' to 'Asc("1") To Asc("9")'. Next, in the same Select block, add a case for zero itself, 'Case Asc("0")'. Inside that, put: If blnAllowZero = False Then KeyAscii = 0 'Block 0 if not allowed

    Lastly, in the Change sub, add the following at the end, just before End Sub: blnAllowZero = (Len(Text1) > 0)


    Hope you come right. :>

    eldren is offline   Reply With Quote
    Old 09-06-2006, 10:15 AM   #10 (permalink)
    Registered User
     
    Join Date: Jun 2006
    Posts: 1
    Thanks: 0
    Thanked 0 Times in 0 Posts
    Would this work

    You could try this it basicly makes your textbox an interger
    Code:
    Private Sub Button_Click()
    
        Dim number As Integer
            'here the varible number is dimed as a interger (number)
            
        On Error Resume Next
            'if it errors which it will if anything but a number is entered_
             resume next
            
        number = Text1.Text
            'text is number which is a interger numbers only
            
        If Err <> 0 Then MsgBox ("number only") Else  
            'if it errors then message else do what you want it to do
    End Sub
    

    Last edited by hell_flame85; 09-06-2006 at 10:17 AM..
    hell_flame85 is offline   Reply With Quote
    Reply

    Breadcrumb
    Go Back   HEXUS.community discussion forums > HEXUS.help - buying advice & technical queries > Operating systems & applications > Software and web development


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Trackbacks are On
    Pingbacks are On
    Refbacks are On




    All times are GMT. The time now is 08:17 AM.

    Any representations/statements made on the HEXUS.community discussion forums are the representations/statements of the author i.e. the person/organisation making them. If any such representations/statements are disputed they are a matter between the parties concerned.
    HEXUS Limited accepts no responsibility for any misrepresentations, inaccurate or false statements made by any person/organisation other than HEXUS Limited employees.
    For more information please read HEXUS Limited's terms, conditions and privacy policy.

    Hosted Exchange | Virtual Dedicated Server

    Powered by vBulletin® Version 3.8.4
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
    Content Relevant URLs by vBSEO 3.3.2
    Copyright © 2010 HEXUS® Limited. All rights reserved. Unauthorised reproduction strictly prohibited.