Masked TextBox

  • Thread starter Thread starter Meelis Lilbok
  • Start date Start date
M

Meelis Lilbok

Hi

Is it possible set mask in masked textbox for selected chars only?
For example numbers 0-9 and letters A-F (hex. chars only)


Best Regards;
Mex
 
not sure if you can do it as part of the mask itself, but obviously you
could do something like the follwing in the TextChanged event...

Private Sub CheckIfHex(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MaskedTextBox1.TextChanged
Dim allowedchars As String = "1234567890abcdefABCDEF"
Dim i As Integer = 0
For i = 0 To MaskedTextBox1.Text.Length - 1
If Not allowedchars.IndexOf(MaskedTextBox1.Text.Substring(i, 1))
MsgBox("Not a valid hex character")
End If
Next
End Sub

James
 
not sure if you can do it as part of the mask itself, but obviously you
could do something like the follwing in the TextChanged event...

Private Sub CheckIfHex(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MaskedTextBox1.TextChanged
Dim allowedchars As String = "1234567890abcdefABCDEF"
Dim i As Integer = 0
For i = 0 To MaskedTextBox1.Text.Length - 1
If Not allowedchars.IndexOf(MaskedTextBox1.Text.Substring(i, 1))> 0 Then

MsgBox("Not a valid hex character")
End If
Next
End Sub

James

I would prefer using the keypress or keydown event for the textbox and
stop any non-hex digits there. Then you would just have to stop the
user from pasting in the inappropriate characters (you could override
WndProc and kill the WM_PASTE message).

Let me know if you need some sample code.

Thanks,

Seth Rowe
 
i did so

Private Sub txtKey_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles txtKey.KeyDown
e.SuppressKeyPress = Not ((e.KeyCode >= Windows.Forms.Keys.A And
e.KeyCode <= Windows.Forms.Keys.F) _
Or (e.KeyCode >= Windows.Forms.Keys.NumPad0 And e.KeyCode <=
Windows.Forms.Keys.NumPad9) _
Or (e.KeyCode >= Windows.Forms.Keys.D0 And e.KeyCode <=
Windows.Forms.Keys.D9) _
Or e.KeyCode = Windows.Forms.Keys.Left Or e.KeyCode =
Windows.Forms.Keys.Right)
End Sub

and shortcuts of course disabled

Mex
 
Back
Top