|
-
Dec 26th, 2009, 08:11 PM
#1
Thread Starter
Addicted Member
TextBox letter disable
How to disable to enter some letters (a,b,c,d...) in textbox? I want to can write just numbers and colon ( in textbox. Thanks.
-
Dec 26th, 2009, 08:28 PM
#2
Fanatic Member
Re: TextBox letter disable
Maybe a regex match would be easiest for this.
-
Dec 26th, 2009, 08:43 PM
#3
Hyperactive Member
Re: TextBox letter disable
You can use the Keydown Event:
Code:
Private Sub Textbox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Textbox1.KeyDown
If (e.KeyData >= 48 And e.KeyData <= 57) Or e.KeyData = 65722 Then
MyBase.OnKeyDown(e)
Else
e.SuppressKeyPress = True
End If
End Sub
65722 = Colon Key
48-57 = 0 to 9 keys
-
Dec 26th, 2009, 09:21 PM
#4
Fanatic Member
Re: TextBox letter disable
Oh yeah, that was a smart idea ^.^
But are you sure that 65722 is colon?
In my list of Charcodes, colon is 44
And japanese colon is 12289
I just think that 65722 seems so far away :P
-
Dec 26th, 2009, 09:35 PM
#5
Hyperactive Member
Re: TextBox letter disable
That is the e.keydata value. I wrote a little app that has a textbox and 3 labels - one each for keycode, keydata and keyvalue. That way I can easily get the values for each, so that should be right.
-
Dec 27th, 2009, 05:30 AM
#6
Thread Starter
Addicted Member
Re: TextBox letter disable
It doesn't work :'(
I can enter numbers but can't colon ( : )
I tried with 58, but it's not.
Any help?
-
Dec 29th, 2009, 12:52 PM
#7
Re: TextBox letter disable
Check the link in my signature on textbox restrictions.
-
Dec 29th, 2009, 01:19 PM
#8
Re: TextBox letter disable
Bear in mind that the keydown method won't trap if anyone pastes text into the textbox so you'll need to add extra validation before you use the contents if you are just relying on the keydown approach.
-
Dec 29th, 2009, 04:19 PM
#9
Re: TextBox letter disable
Code:
Private Sub TextBox1_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles TextBox1.TextChanged
For Each ch As Char In TextBox1.Text
If Not Char.IsDigit(ch) Then
If Not ch = ":" Then
'bad character
End If
End If
Next
End Sub
-
Dec 29th, 2009, 04:24 PM
#10
Re: TextBox letter disable
This is generally a bad approach. You don't need to filter user's input you need to validate it after the user is confirmed it by either clicking some button or pressing the Enter key.
-
Dec 29th, 2009, 04:27 PM
#11
Re: TextBox letter disable
Validating as entering can be OK, but a lot of the time it isn't. I have been wondering if hepeci is working with time values?
Last edited by dbasnett; Dec 29th, 2009 at 04:31 PM.
-
Apr 23rd, 2011, 04:21 PM
#12
Fanatic Member
Re: TextBox letter disable
I just used dkahn's code in post #3 and found it very useful. I modified it like this
Code:
' 0 to 9 Backspace 0 to 9 on numberpad mousewheel up, down, PageUp, PageDown, End, Home, left, right, up and down arrows.
If (e.KeyData >= 48 And e.KeyData <= 57) Or e.KeyData = 8 Or (e.KeyData >= 96 And e.KeyData <= 105) Or (e.KeyData >= 33 And e.KeyData <= 40) Or e.KeyData = 46 Then
MyBase.OnKeyDown(e) ' e.KeyData = 46 for Delete
Else
e.SuppressKeyPress = True
End If
That way the numberpad digits 0 to 9 are included as well as backspace, mousewheel, and the others mentioned above in the comments. Colon isn't included here.
I also used dbasnett's code in case someone decides to paste into the combobox(not textbox for my situation) in question.
Last edited by EntityX; Apr 25th, 2011 at 09:41 PM.
 Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.
"Persistence is the magic of success." Paramahansa Yogananda
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
|