| View previous topic :: View next topic |
| Author |
Message |
rainster Newbie
Joined: 04 Nov 2003 Posts: 4
|
Posted: Nov 4th, 2003 06:29 PM Post subject: Limiting Textbox Input |
|
|
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 |
|
ritchieroo Newbie
Joined: 27 Oct 2003 Posts: 8
|
Posted: Nov 5th, 2003 04:58 AM Post subject: |
|
|
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 |
|
Avis Junior Poster

Joined: 07 Oct 2003 Posts: 510 Location: India
|
|
| Back to top |
|
P.T.A.M. Administrator

Joined: 08 Oct 2003 Posts: 752 Location: Greece
|
Posted: Nov 5th, 2003 03:40 PM Post subject: |
|
|
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 |
|
rainster Newbie
Joined: 04 Nov 2003 Posts: 4
|
Posted: Nov 7th, 2003 04:39 AM Post subject: |
|
|
| Thanks everyone. I'll try all of the options and see which one works best. |
|
| Back to top |
|
|