-
July 13th, 2007, 03:48 PM
#1
Numerical in textbox
Hi All ,
i have a textbox and i need to limit the user input to this textbox to numerical values ,
so he cannot type strings just 1 2 3 and so on ,
i mena like the windwos calculator doens't accept strings or such charchters ,
how to do that ?
-
July 13th, 2007, 04:11 PM
#2
Re: Numerical in textbox
Take a look here: How to: Create a Numeric Text Box .
A very simple solution is to handle the textbox keypress event:
Code:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !(Char.IsDigit(e.KeyChar) );
}
Laitinen
Last edited by laitinen; July 13th, 2007 at 04:14 PM.
-
July 13th, 2007, 04:37 PM
#3
-
July 13th, 2007, 04:46 PM
#4
Re: Numerical in textbox
i tried it , but it doens't work ,
even there are no errors , that means my appliaction accepts it but the textbox still accept both numeric and string numbers ,
-
July 13th, 2007, 04:54 PM
#5
Re: Numerical in textbox
1) Which approach did you try?
2) Show the code you used.
-
July 13th, 2007, 04:56 PM
#6
Re: Numerical in textbox
i seem to recall .net2 offering a masked text bos as a component that would do it i would suppose
-
July 13th, 2007, 04:59 PM
#7
Re: Numerical in textbox
I think it has to be like this:
Code:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = Char.IsDigit(e.KeyChar);
}
(Without the !)
Anyway here's another one that I built for my last application, it worked for me...
Code:
using System.Text.RegularExpressions;
...
private void t_KeyDown(object sender, KeyEventArgs e)
{
if (!Regex.IsMatch(e.KeyCode.ToString(), @"\d"))
{
e.SuppressKeyPress = true;
}
}
[Edit: oops, forgot to remove the backspace and spacebar...
I edited the code]
Last edited by Talikag; July 13th, 2007 at 05:02 PM.
-
July 13th, 2007, 05:06 PM
#8
Re: Numerical in textbox
Originally Posted by Talikag
I think it has to be like this:
Code:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = Char.IsDigit(e.KeyChar);
}
(Without the !)
No, you want to set the key press to handled only if the character is not a decimal digit. If it is a decimal digit you want the key press to be handled and added to the textbox.
Laitinen
-
July 13th, 2007, 05:10 PM
#9
Re: Numerical in textbox
Originally Posted by laitinen
No, you want to set the key press to handled only if the character is not a decimal digit. If it is a decimal digit you want the key press to be handled and added to the textbox.
Laitinen
isn't e.Handled = true means the KeyChar will NOT appear in the textbox? That's what I thought so far...
-
July 13th, 2007, 05:14 PM
#10
Re: Numerical in textbox
Originally Posted by Talikag
isn't e.Handled = true means the KeyChar will NOT appear in the textbox? That's what I thought so far...
Yes that is right. And you do not want it to appear if it is not a digit, right?
-
July 13th, 2007, 05:21 PM
#11
Re: Numerical in textbox
Originally Posted by laitinen
Yes that is right. And you do not want it to appear if it is not a digit, right?
Yea, I got it. It's confusing :S
Emm... Anyway... Both ways should work. The problem is probably the fact that you didn't add an event handler in the *.disigner.cs, like this:
Code:
this.textBox1.Click += new System.EventHandler(this.textBox1_Click); //Not in this case, it is just an example.
-
July 13th, 2007, 10:49 PM
#12
Re: Numerical in textbox
You can use NumUpDown Control.
Using KeyPress is also right. just add the handle for the event
Code:
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
-
July 14th, 2007, 01:12 AM
#13
Re: Numerical in textbox
even though its ugly I'd recomend using the up/down control too...
if you're going to make a numeric only textbox you have to also watch for paste...
its easier in .net 2.0 because they added the SuppressKeyPress but I wrote a numeric only textbox control a long time ago that you could take a look at. I overrode the PreProcessMessage, but you can do pretty much the same thing in the OnKeyDown event and set supress key to true if its not numeric. handling the paste operation will be the same as what I wrote though.
-
July 14th, 2007, 04:40 AM
#14
Re: Numerical in textbox
Originally Posted by MadHatter
even though its ugly I'd recomend using the up/down control too...
Yes it is very ugly indeed! And not very useful at all in many situations. How would a numeric up/down control work if the number has a large range and/or needs high accuracy??
Imagine you need to fill in how much tax you payed last year. You will need the accurate sum and you will need a range from 0 to sevaral millions, with a step of 1 dollar. Up/down control is useless here.
So as I have said many times, a numeric up/down control might be a good solution, but it is highly dependent on the nature of the number you want to store.
Cheers,
Laitinen
-
July 14th, 2007, 05:06 AM
#15
Re: Numerical in textbox
I like the idea of Rudegar ( the Masked Textbox ) too, it can be easily customised for your purpose. I'd try that first before trying a Numeric Updown.
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
|