Reputation: 21
I need to create a text box when the user press the key will fill the pattern:
[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}
A valid example is:
63CDB75C-D58A-4100-8C24-9E6433E263B0
The user can only press A..F and 0..9 on the groups 8-4-4-4-12 total digits.
But I don't know how to format it on the vb6 text box. can you help me?
Upvotes: 2
Views: 499
Reputation: 9726
The easy way is to check the KeyPress event and decide if the key is valid or not. It appears you have a well defined format so I added a textbox to a form and set it's maximum length to 36. I added code in the KeyPress event to check for valid keys. I also added code in the Changed event to append the hyphen to the current text instead of making the user enter it. Of course this is very simple so you may need to add to it to work specifically for you. For instance as it is now you can't paste into the textbox with Ctrl + V. Further, the hyphen key isn't currently allowed so if they delete a hyphen they will have to start over to get it back. Also there is no Error handler.
Private Sub Text1_Change()
If Len(Text1.Text) = 8 _
Or Len(Text1.Text) = 13 _
Or Len(Text1.Text) = 18 _
Or Len(Text1.Text) = 23 Then
Text1.Text = Text1.Text & "-"
Text1.SelStart = Len(Text1.Text)
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase$(Chr$(KeyAscii))) 'force all characters to upper case
Select Case KeyAscii
Case 8, 48 To 57, 65 To 70 '8 is the backspace, 48 to 57 is 0 to 9, 65 to 70 is A to F
Case Else
KeyAscii = 0 'Cancel the key
End Select
End Sub
Upvotes: 1