Only allow for a double in textbox
-
I have looked on google and I can't find anything that does what I need in an example.. I want to validate the textbox on keypress and make it where the user(me) can't enter anything other than a price value. like 1.99 2.99 whatever.. I have it working so far as to only allow numbers a backspace and the '.' but I can't figure out how to restrict it to only 2 places after the decimal. Any help would be great. thanks.. Here is what I have so far...
private void Price_KeyPress(object sender, KeyPressEventArgs e)
{e.Handled = !Char.IsNumber(e.KeyChar) && (e.KeyChar != '\\b')&& (e.KeyChar != '.'); }
-
I have looked on google and I can't find anything that does what I need in an example.. I want to validate the textbox on keypress and make it where the user(me) can't enter anything other than a price value. like 1.99 2.99 whatever.. I have it working so far as to only allow numbers a backspace and the '.' but I can't figure out how to restrict it to only 2 places after the decimal. Any help would be great. thanks.. Here is what I have so far...
private void Price_KeyPress(object sender, KeyPressEventArgs e)
{e.Handled = !Char.IsNumber(e.KeyChar) && (e.KeyChar != '\\b')&& (e.KeyChar != '.'); }
-
I have looked on google and I can't find anything that does what I need in an example.. I want to validate the textbox on keypress and make it where the user(me) can't enter anything other than a price value. like 1.99 2.99 whatever.. I have it working so far as to only allow numbers a backspace and the '.' but I can't figure out how to restrict it to only 2 places after the decimal. Any help would be great. thanks.. Here is what I have so far...
private void Price_KeyPress(object sender, KeyPressEventArgs e)
{e.Handled = !Char.IsNumber(e.KeyChar) && (e.KeyChar != '\\b')&& (e.KeyChar != '.'); }
as Jimmanuel said you could use the numeric... But I have found some small bugs in the numeric up down such: as you introduce some number then delete it and if you get the .Value it's not null or zero or... So, I had this problem too. Sorry I can find the code right now nor the time to write it but this is how you could do it: use the KeyDown event and then put the logic:
//check if it already contains a dot '.' character by using the text property
//if it does then allow only number and refuse everything else using
e.Handled=true;
e.SuppressKeyPress=true;
//also check the number of chars after the '.' sign//else allow dot and numbers.
use
e.KeyCode == Keys.OemPeriod
to check for '.' hope it helps
-
Use a
NumericUpDown
instead. It accepts only numeric input and you can specify in the designer how many decimal places it should use - no custom coding necessary.:badger:
Not quite, you can enter more decimals, its only when you Leave the Control that it gets rounded to the specified number of decimals. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
as Jimmanuel said you could use the numeric... But I have found some small bugs in the numeric up down such: as you introduce some number then delete it and if you get the .Value it's not null or zero or... So, I had this problem too. Sorry I can find the code right now nor the time to write it but this is how you could do it: use the KeyDown event and then put the logic:
//check if it already contains a dot '.' character by using the text property
//if it does then allow only number and refuse everything else using
e.Handled=true;
e.SuppressKeyPress=true;
//also check the number of chars after the '.' sign//else allow dot and numbers.
use
e.KeyCode == Keys.OemPeriod
to check for '.' hope it helps
just checking the latest keystroke as if it comes at the right of what is already there isn't sufficient: 1. the caret might not be at the end 2. you may want to support: copy, cut, paste, backspace... all of these require more than just checking the new character. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
just checking the latest keystroke as if it comes at the right of what is already there isn't sufficient: 1. the caret might not be at the end 2. you may want to support: copy, cut, paste, backspace... all of these require more than just checking the new character. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
true. but I checked for stuff like that(if it's the delete key, back key etc) As for the position of the caret/cursor I didn't really cared cause I transformed .8686 in 0.8686 It really works for me. What I said here/before are some "guide lines". Again sorry I can't find the code. Just the *.dll. Thanks for the correction. I mean it. It should help TonyOnLinux too if he decides to implement he's own logic/control.
-
I have looked on google and I can't find anything that does what I need in an example.. I want to validate the textbox on keypress and make it where the user(me) can't enter anything other than a price value. like 1.99 2.99 whatever.. I have it working so far as to only allow numbers a backspace and the '.' but I can't figure out how to restrict it to only 2 places after the decimal. Any help would be great. thanks.. Here is what I have so far...
private void Price_KeyPress(object sender, KeyPressEventArgs e)
{e.Handled = !Char.IsNumber(e.KeyChar) && (e.KeyChar != '\\b')&& (e.KeyChar != '.'); }
A while ago I wrote this article[^] for a simple numeric text box. It would need some alteration to allow a decimal point and to check the number is valid for your requirements (at the moment it only allows numeric input i.e. it is still a text box and not a NumberBox) but it could serve as a starting point. It handles copy and paste via keyboard/mouse and all edit keys etc...
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
I have looked on google and I can't find anything that does what I need in an example.. I want to validate the textbox on keypress and make it where the user(me) can't enter anything other than a price value. like 1.99 2.99 whatever.. I have it working so far as to only allow numbers a backspace and the '.' but I can't figure out how to restrict it to only 2 places after the decimal. Any help would be great. thanks.. Here is what I have so far...
private void Price_KeyPress(object sender, KeyPressEventArgs e)
{e.Handled = !Char.IsNumber(e.KeyChar) && (e.KeyChar != '\\b')&& (e.KeyChar != '.'); }
Why not using a ConvertEventHandler delegate ?
-
I have looked on google and I can't find anything that does what I need in an example.. I want to validate the textbox on keypress and make it where the user(me) can't enter anything other than a price value. like 1.99 2.99 whatever.. I have it working so far as to only allow numbers a backspace and the '.' but I can't figure out how to restrict it to only 2 places after the decimal. Any help would be great. thanks.. Here is what I have so far...
private void Price_KeyPress(object sender, KeyPressEventArgs e)
{e.Handled = !Char.IsNumber(e.KeyChar) && (e.KeyChar != '\\b')&& (e.KeyChar != '.'); }
You need only to check the text of textbox. My textbox is name tAmount
private void tAmount_KeyPress(object sender, KeyPressEventArgs e)
{
int isNumber = 0;
Update();
string tmp = Convert.ToString(tAmount.Text);
if (tmp.Contains(".") == true)
e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber) && (e.KeyChar != '\b');
else
e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber) && (e.KeyChar != '\b') && (e.KeyChar != '.');}