473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Restricting a windows textbox to digits only.

Restricting a windows textbox (edit item) to digits only.

Is there a windows option, using .NET C#, to only allow a user to enter
digits ONLY? If so, what is the flag or setting? If no, what is the best
method to manually and robustly do it?

I have a windows form with an editbox that I only want user's to enter
digits.

Thanks in advance.
Nov 17 '05 #1
3 4651
Hi volume,
you could capture the keypress event of the textbox and validate that the
character was a digit i.e.

private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.KeyPress += new
KeyPressEventHandler(textBox1_KeyPress);
}

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}

Hope that helps
Mark R Dawson
http://www.markdawson.org


"volume" wrote:
Restricting a windows textbox (edit item) to digits only.

Is there a windows option, using .NET C#, to only allow a user to enter
digits ONLY? If so, what is the flag or setting? If no, what is the best
method to manually and robustly do it?

I have a windows form with an editbox that I only want user's to enter
digits.

Thanks in advance.

Nov 17 '05 #2
You can use the Validating event to validate the contents of the TextBox.
User can't select anything and even not close the form while the contents is
not validated.
textBox1.Validating += new
System.ComponentModel.CancelEventHandler(textBox1_ Validating);

private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
e.Cancel = !Regex.IsMatch(textBox1.Text, "[0-9]*");
}

"volume" <a@a.com> wrote in message
news:ek**************@TK2MSFTNGP14.phx.gbl...
Restricting a windows textbox (edit item) to digits only.

Is there a windows option, using .NET C#, to only allow a user to enter
digits ONLY? If so, what is the flag or setting? If no, what is the best
method to manually and robustly do it?

I have a windows form with an editbox that I only want user's to enter
digits.

Thanks in advance.

Nov 17 '05 #3
Thanks all. They both worked ace.

"Lebesgue" <no****@spam.jp> wrote in message
news:O5**************@TK2MSFTNGP09.phx.gbl...
You can use the Validating event to validate the contents of the TextBox.
User can't select anything and even not close the form while the contents is not validated.
textBox1.Validating += new
System.ComponentModel.CancelEventHandler(textBox1_ Validating);

private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
e.Cancel = !Regex.IsMatch(textBox1.Text, "[0-9]*");
}

"volume" <a@a.com> wrote in message
news:ek**************@TK2MSFTNGP14.phx.gbl...
Restricting a windows textbox (edit item) to digits only.

Is there a windows option, using .NET C#, to only allow a user to enter
digits ONLY? If so, what is the flag or setting? If no, what is the best
method to manually and robustly do it?

I have a windows form with an editbox that I only want user's to enter
digits.

Thanks in advance.


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
18140
by: Rookie | last post by:
I need to display several columns of numbers in a textbox. I wish to display the columns with the decimal point position aligned vertically. I have found that the # digit placeholders do not...
5
454
by: Robin | last post by:
H In VB6 I used to trap by keycode on the keydown event and if the keystroke didn't fit into my criteria I would set the value of the keycode to 0 which would invalidate the keystroke. I noticed...
3
488
by: vagos | last post by:
Can someone please, tell me how to check the value of a textbox in a form, 1, the value must be only a positive number ( not 34ax43) , for input a stock number of product 2, the value must be...
2
6793
by: G | last post by:
I would like to restrict the number of digits in a number field in the design of a table. eg. 16 digits maximum in a certain number field of a table. thanks.
1
2406
by: mdb | last post by:
I'm trying to restrict the characters that are valid in a textBox, so I derived from System.Windows.Forms.TextBox, and overrode WndProc, catching the key up/down messages... Everything works...
6
3690
by: Altramagnus | last post by:
I have searched throught the newsgroups on how to restrict entry in textboxes, for example, I only want the textbox to only accept numberic. The standard answer is to use the KeyPress event....
3
7470
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work...
2
2190
by: hollykatong | last post by:
hello, is there a way to restricting a textbox input wherein it will only accept two integer with decimal places? for example if i enter 90 or 90.1234 it will allow me and when i try to type 890...
0
1026
by: lagu2653 | last post by:
I've made a custom textbox with a certain background. In order to the textbox higher than normal I set the property multiline to true. The row above and below are showed since the textbox is higher...
0
7198
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,...
0
7072
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...
0
7271
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,...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6979
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...
0
4666
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...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
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 ...
0
373
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...

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.