By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,117 Members | 1,093 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,117 IT Pros & Developers. It's quick & easy.

Limit textbox to numeric keys

P: n/a
How do I go about limiting a textbox to accepting numeric key input from the keyboard in vb.net?


--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"ScareCrow" <no*****@justhere.com> schrieb:
How do I go about limiting a textbox to accepting numeric key input from
the keyboard in vb.net?


<URL:http://groups.google.de/groups?selm=OFw5dO1kEHA.2504%40TK2MSFTNGP14.phx.gb l>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2

P: n/a

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote
How do I go about limiting a textbox to accepting numeric key input from
the keyboard in vb.net?


<URL:http://groups.google.de/groups?selm=OFw5dO1kEHA.2504%40TK2MSFTNGP14.phx.gb l>

Would you really rather use an expensive error trap when you can include
the test yourself? Testing it with code does not induce the delay you get while
the error is being caught....

Dim Source As TextBox = DirectCast(sender, TextBox)
Dim Message As String
If Source.Text Like "*[!0-9.]*" Then ' Remove '.' if decimals are not allowed.
Message = "Text is not expected here." & vbCrLf & "Enter a numeric value."
End If
Me.ErrorProvider1.SetError(Source, Message)

???
LFS

Nov 21 '05 #3

P: n/a
Scare,

I made a sample after a long discussion in this newsgroup, the best of two
methods in my opinion.

http://groups-beta.google.com/group/...aa417bf7a25528

(It limits as well the lenght, however accepts as well decimal points and
exponents)
When you want only true numeric characters, than you get more a sample as
Herfried and Larry's.

Another advantage of this one is that it test during typing and not wait
until the end. I have somewhere one which does it during typing as well,
there are given a lot for that in this newsgroup, however that is in my
opinion easy to make as well from my sample combining with the code from
Larry.

I hope this helps?

Cor
How do I go about limiting a textbox to accepting numeric key input from
the keyboard in vb.net?


--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-

Nov 21 '05 #4

P: n/a
Larry,

"Larry Serflaten" <se*******@usinternet.com> schrieb:
Would you really rather use an expensive error trap when you can include
the test yourself? Testing it with code does not induce the delay you
get while
the error is being caught....

Dim Source As TextBox = DirectCast(sender, TextBox)
Dim Message As String
If Source.Text Like "*[!0-9.]*" Then ' Remove '.' if decimals are
not allowed.
Message = "Text is not expected here." & vbCrLf & "Enter a
numeric value."
End If
Me.ErrorProvider1.SetError(Source, Message)


That would work too and is a more evolved soltion :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5

P: n/a
Try this:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If (IsNumeric(e.KeyChar) = False) Then e.Handled = True
End Sub

"ScareCrow" wrote:
How do I go about limiting a textbox to accepting numeric key input from the keyboard in vb.net?


--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.