473,320 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to set the property of the textbox to numbers only?

I want to enter only numbers in a textbox. How can I set the property of the textbox?
Feb 28 '11 #1
5 17511
for this purpose i use KEY_PRESS event of textBox and just place the following code in it...

if(char.IsNumber(e.KeyChar)!=true)
e.Handled=true;

/*If the event is not handled, it will be sent to the operating system for default processing. Set Handled to true to cancel the KeyPress event.*/ msdn

for more theory visit...

http://msdn.microsoft.com/en-us/libr...eventargs.aspx

found it while googling hope it helps....
Mar 4 '11 #2
HaLo2FrEeEk
404 256MB
Keep in mind that the above method will disallow the use of the backspace key, as well as (I think) the delete and arrow keys. I wrote some code that transformed a textbox into a numbers-only textbox:

Expand|Select|Wrap|Line Numbers
  1. /// <summary>
  2. /// Processes the data entered into a textbox to determine if it is numeric.
  3. /// </summary>
  4. /// <param name="KeyCode">The KeyCode that was typed into the textbox.</param>
  5. /// <returns>False if the value entered was numeric, otherwise true.</returns>
  6. private bool suppressKeyPress(Keys KeyCode)
  7. {
  8.     if (KeyCode < Keys.D0 || KeyCode > Keys.D9 &&
  9.         KeyCode < Keys.NumPad0 || KeyCode > Keys.NumPad9)
  10.     {
  11.         if (KeyCode != Keys.Back &&
  12.             KeyCode != Keys.Delete &&
  13.             KeyCode != Keys.Left &&
  14.             KeyCode != Keys.Right)
  15.         {
  16.             return true;
  17.         }
  18.     }
  19.  
  20.     if (Control.ModifierKeys == Keys.Shift)
  21.         return true;
  22.  
  23.     return false;
  24. }
The method will return a boolean, true if the keypress should be ignored, false otherwise. Use this method in a KeyDown event handler. I believe you'll have to set your form's KeyPreview property to true for it to work. Within the event handler, simply set the KeyEventArgs.SupressKeyPress property to the return of the above method (which you have to pass a KeyCode value to, get the KeyCode from the KeyEventArgs for the handler).

I think I covered everything, let me know if you need more help.
Mar 4 '11 #3
forgive me for my ignorance on backspace..i corrected it nw

u can use this code in textBox Key_press event

if(char.IsNumber(e.KeyChar)!=true && e.KeyChar != '\b' )
e.Handled=true;

this allows numbers,backspace,left,right,delete arrows in
textBox..

nd for decimals add in IF condition like (&& e.KeyChar !='.' ) code urself for single decimal..

this is upto my knowledge .. forgive me for any mistakes.
Mar 4 '11 #4
HaLo2FrEeEk
404 256MB
Yeah...or he could use the method I wrote which uses proper programming practices and actually works.
Mar 4 '11 #5
use regular expression

System.Text.RegularExpressions.Regex.IsMatch(textB ox1.Text, "[ ^ 0-9]")


or do like this..

if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}

Full Source....Numeric Only Textbox

Richard
Feb 12 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Keith | last post by:
Am I crazy - to be shocked that there is no Numeric, Alpha, and AlphaNumeric Property in on the Textbox control I searched and searched - and found other people's code - but they don't work...
10
by: Tosch | last post by:
I have a combobox where a user can select a zoom factor or enter a zoom factor. I tried to limit entry into the combobox to numbers only by catching the keydown event and setting e.handled = true...
4
by: Steven.Xu | last post by:
Hi, men. It's just a small problem. On previous VB, I can reset the keycode in the KeyDown or KeyUp event. But in VB.NET, The property of parameter "e" is readonly. How can I have a TextBox just...
4
by: wkatz | last post by:
Hi, Gurus. What hashing algorithm outputs hash value as numbers only? For example, if you pass a “John Q. Public” it will output 23324. If there is no such hashing, how hard is it to hire somebody to...
4
by: ghostrider | last post by:
I ned help in figuring out to make my program display even numbers only. My for loop counts every number, but I only need to display the even ones. Can someone help me with this?
1
by: Luzuko | last post by:
I would like to know how can i restrict textbox input in VB.net using code instead of VB.net controls. e.g If i want a user to type numbers only in a textbox(ID number textbox), how can i make...
1
by: shyamsp2007 | last post by:
I want a textbox which only accept multiple commas,single dot and number only.
23
by: mathewgk80 | last post by:
Hi all, I would like to get the Javascript code to check the textbox contains only numeric values or numbers.. I am using asp.net and c#.net.. Please help me.. Regards, Mathew
6
by: krishnaneeraja | last post by:
Hi, Iam developing windows application using vb.net with c#.net.In this i want to validate textbox accept only negative float numbers like -2.3 etc.... please help me.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.