|
-
Jun 22nd, 2006, 10:59 AM
#1
Thread Starter
New Member
[RESOLVED] Masked Text Box - Limit to Hex Values
I would like the user to enter a hex number in a text box in the format of
0x#####
For example, 0x12AD would be a valid entry. It would be nice to be able to mask all non valid hex values (non 0 to 9, A-F) with the MaskTextBox control. Can anyone help me set up the proper mask to do so (if its even possible)?
Regards,
Kevin
-
Jun 22nd, 2006, 07:07 PM
#2
Hyperactive Member
Re: Masked Text Box - Limit to Hex Values
I don't know if you want to handle the cases where the user leaves off the 0x at the start or anything, so I'll pretend you want that 0x everytime for now (makes the code easier)
This code needs to be in textbox1_TextChanged
VB Code:
dim stras string = textbox1.text
if not (isvalidhex(str))
' Do error handling stuff
And now the helper routine
VB Code:
private sub isvalidhex(byref str as string)
if not str.substring(0,2) = "0x"
return false
end if
for i = 2 to str.length - 1
if not (isnumeric(str.substring(i-1,1) _
or tolower(str.substring(i-1,1)) = "a" _
or tolower(str.substring(i-1,1)) = "b" _
or tolower(str.substring(i-1,1)) = "c" _
or tolower(str.substring(i-1,1)) = "d" _
or tolower(str.substring(i-1,1)) = "e" _
or tolower(str.substring(i-1,1)) = "f")
return false
next
return true
End Sub
I just threw this together, so there may be some errors, but as long as you understand what's going on, you should be able to fix it. Any problems, post again and I'll double check it all.
-
Jun 22nd, 2006, 07:30 PM
#3
Re: Masked Text Box - Limit to Hex Values
I don't think that there is a specific mask that will do it on its own because you can only specify types of characters that are valid, not specific characters. You'd have to provide your own masking language for the control through its MaskedTextProvider property. I know that that's possible but I don't know how it's done. The simple way is to use this mask: "\0x>AAAAA". This will place a literal "0x" in the field followed by five positions that will accept any alphanumeric input and convert it to upper case. You'd then use this code to exclude invalid letters:
VB Code:
Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MaskedTextBox1.KeyDown
If e.KeyCode > Keys.F AndAlso _
e.KeyCode <= Keys.Z AndAlso _
Not e.Control AndAlso _
Not e.Alt Then
'The user has pressed a letter key greater than F, which would be allowed by the mask, so reject it.
e.SuppressKeyPress = True
If Me.MaskedTextBox1.BeepOnError Then
My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
End If
End If
End Sub
I tried using the KeyPress event but it seemed not to work for some reason.
-
Jun 23rd, 2006, 07:55 AM
#4
Thread Starter
New Member
Re: Masked Text Box - Limit to Hex Values
Excellent. Thank you both for the responses. I was hoping I could just use the Mask propery to exlude specific characters, but looks like I was wrong. Your ways certainly work.
Thanks again,
Kevin
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|