Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » General

Post new topic   Reply to topic
Limiting Textbox Input
View previous topic :: View next topic  
Author Message
rainster
Newbie


Joined: 04 Nov 2003
Posts: 4

PostPosted: Nov 4th, 2003 06:29 PM    Post subject: Limiting Textbox Input Reply with quote

Hi!

I was wondering if someone might know the proper way to set what may or may not be entered into a text box?

Example: I have a program that I would like to improve on. It converts hex numbers to decimal or vice versa.

How can I can make that text box only accept a hexadecimal number, 8 digits in length, and ignore/not accept any other jibberish characters?

Thanks in advance for your help!
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ritchieroo
Newbie


Joined: 27 Oct 2003
Posts: 8

PostPosted: Nov 5th, 2003 04:58 AM    Post subject: Reply with quote

I would probably use the Validate event. It doesn't stop invalid characters from being entered, as it fires just before the control loses focus.

Code:

Private Sub Text1_Validate(Cancel As Boolean)
    Dim lngIndex As Long
    Dim strChar As String
   
    With Text1
        For lngIndex = 1 To Len(.Text)
            strChar = Mid$(.Text, lngIndex, 1)
       
            If Not (strChar Like "[0-9A-Fa-f]") Then
                Cancel = True
                ' Add message box here perhaps
            End If
           
            If Cancel Then Exit For
        Next
   
        If Not Cancel Then
            .Text = UCase$(.Text)
        End If
    End With
End Sub


I have tried the alternative before, which is to try and use the KeyDown or KeyPress event to stop invalid characters from being entered to start with, but I found it quicky became messy.

The beauty of the Validate event is that you can control when the event gets fired by setting the CausesValidation property on other Controls. This means that you can have a "Cancel" button on a dialog that is always clickable, even if your field contains invalid data.
Back to top
View user's profile Send private message
Avis
Junior Poster


Joined: 07 Oct 2003
Posts: 510
Location: India

PostPosted: Nov 5th, 2003 05:07 AM    Post subject: Reply with quote

Hi!

Better check this out: http://www.avissoft.com/showthread.php?s=&threadid=36

Hope this helps!

Thanks!
_________________
Code Snippets, Tutorials, Utilities, Controls

Low cost Web Hosting
Hosting starts at as low as $4 per year!


Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags!
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Nov 5th, 2003 03:40 PM    Post subject: Reply with quote

You could also use the Change event or one of the keydown/keypress and check the value of keycode/keyascii...
_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
rainster
Newbie


Joined: 04 Nov 2003
Posts: 4

PostPosted: Nov 7th, 2003 04:39 AM    Post subject: Reply with quote

Thanks everyone. I'll try all of the options and see which one works best.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » General All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic