473,566 Members | 3,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Limit textbox to numeric keys

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
5 4079
"ScareCrow" <no*****@justhe re.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.d e/groups?selm=OFw 5dO1kEHA.2504%4 0TK2MSFTNGP14.p hx.gbl>

--
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

"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.d e/groups?selm=OFw 5dO1kEHA.2504%4 0TK2MSFTNGP14.p hx.gbl>

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(send er, 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.ErrorProvide r1.SetError(Sou rce, Message)

???
LFS

Nov 21 '05 #3
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
Larry,

"Larry Serflaten" <se*******@usin ternet.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(send er, 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.ErrorProvide r1.SetError(Sou rce, 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
Try this:

Private Sub TextBox1_KeyPre ss(ByVal sender As Object, ByVal e As
System.Windows. Forms.KeyPressE ventArgs) Handles TextBox1.KeyPre ss
If (IsNumeric(e.Ke yChar) = 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3336
by: MFRASER | last post by:
Is there a limit to the size of a key for a Hashtable. I am adding items to a hashtable using a string for the key and it seems that when my key is 16 characters long the indexer does not work properly. If I truncate the key to be 12 characters the indexer works. Also is there a better way to build a key for a string, should I convert the...
11
4523
by: Keith | last post by:
I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely reading them all - but I think I have a differing opinion of how I want to handle the 'user experience' in the application I'm creating. While I know I could allow the user to enter in number and alpha...
5
2088
by: Powerguy | last post by:
Hi, I am trying to allow users to only be able to enter numeric numbers into a textbox. The problem is that I cannot move focus with the tab key even if I allow it as an allowable key stroke. I have successfully achieved this thanks to abit of code I found from within these forums (thanks who ever posted it!): Protected Overrides...
3
6670
by: Aaron Smith | last post by:
I found an quick and dirty example of a numeric text box that converts the string to a currency mask.. Here is the code: Public Class NumericMaskedTextBox Inherits System.Windows.Forms.TextBox Private Sub NumericMaskedTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress 'the...
1
8241
by: Dino Buljubasic | last post by:
Hi, Can somebody help me with this problem please. I need to get the value, not just to check for it, of a number pressed but it has to work for both, numbers above QUERTY and numbers entered from numeric pad. I know how to check for the keys (which I am doing in the code below). What I need is the value (e.g if D2 or NumPad2 then '...
6
3175
by: Avi G | last post by:
Hi, what is the code to limit a textbox that will accept only text and not number private void button1_Click(object sender, EventArgs e) { if (textBox1.Text = "numbers") { // do something }
5
13074
by: Reny | last post by:
can any one tell how can i restrict my user to type just numeric character in the textbox.I am using VS.NET 2003 (VB.NET)
0
1247
by: EgoSum | last post by:
Can someone help me with custom text box? I want change behavior custom date text box - disallow entry and pass entry from numeric keyboard to a text box. Code below disallow entry, but how I can pass entry? public class myDateTextBox : AMS.TextBox.DateTextBox { protected override bool ProcessDialogKey(Keys keyData) {if (keyData >=...
2
1889
by: EgoSum | last post by:
Can someone help me with custom text box? I want change behavior custom date text box - disallow entry and pass entry from numeric keyboard to a text box. Code below disallow entry, but how I can pass entry? public class myDateTextBox : AMS.TextBox.DateTextBox { protected override bool ProcessDialogKey(Keys keyData) {if (keyData >=...
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.