I've got a minor modification that will only allow 2-digits after the decimal point, without a regex and no for loop. Can't cut 'n past, too many characters. Drop me a line if you're interested.
Nice, but what about negative numbers? I came up with the following code, which toggle the negative symbol when minus is pressed. The caret (prompt) is always sent at the end of the string. if (ch == '-') { if (Text.Length != 0 && Text[0] == '-') { Text = Text.Substring(1); //If we have a minus as first character, it removes it } else { Text = "-" + Text; } SelectionStart = Text.Length; //These 2 lines send the caret/prompt at the end of the string SelectionLength = 0; //Apparently superfluous, but it doesn't harm e.Handled = true; }
Very helpful. Thank you. It might be usefult o allow for a hyper for negative numbers. One thing it would be nice to cater for is if the user "pastes" into the control. It will let us paste text content in. Also, what if we wanted to use this on more than one control. Can we somehow turn it into a control sow e don't replicate code? Thanks.
Hi Charmy, It's a great video. I like it. do have video on how to convert number no c# database to string and display no windows form ? other question, do you also have video on how to display graph from database in c# using SQL Server
Thanks for the easy solution ;) --> You should use CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0] instead of checking for a dot-char, because in other cultures e.g. in german it will be the ',' char for commas alternative: private void breiteQuer_KeyPress(object sender, KeyPressEventArgs e) { double result; char c = (char)e.KeyChar; string text = tb.Text + c; if (c != (char)Keys.Back && !double.TryParse(text, out result)) { e.Handled = true; } }
How to set textbox can both read and write value ?
how to limit decimal place to 2 only while keypress?
THANK YOU for a really simple solution that I have been looking for to validate my floating point text box.
How can i implement you code in WPF C#? Thank you so much if you can help....
I've got a minor modification that will only allow 2-digits after the decimal point, without a regex and no for loop. Can't cut 'n past, too many characters. Drop me a line if you're interested.
Best answer I have found so far. Good job
Nice, but what about negative numbers? I came up with the following code, which toggle the negative symbol when minus is pressed. The caret (prompt) is always sent at the end of the string.
if (ch == '-')
{
if (Text.Length != 0 && Text[0] == '-')
{
Text = Text.Substring(1); //If we have a minus as first character, it removes it
}
else
{
Text = "-" + Text;
}
SelectionStart = Text.Length; //These 2 lines send the caret/prompt at the end of the string
SelectionLength = 0; //Apparently superfluous, but it doesn't harm
e.Handled = true;
}
Amazing solution to capture prices, thanks dude
Very helpful. Thank you. It might be usefult o allow for a hyper for negative numbers.
One thing it would be nice to cater for is if the user "pastes" into the control. It will let us paste text content in.
Also, what if we wanted to use this on more than one control. Can we somehow turn it into a control sow e don't replicate code?
Thanks.
Did u find a solution to this?
this is what I was looking for, thanks
Nice video! Can you tell me how to add a comma to the filter?
my e.KeyChar doesnt work :S how can i fix that , it gives a error and evrything is right
HOW I MAKE A TEXTBOX THAT ACCEPT ONLY EMAIL?
Hi Charmy, It's a great video. I like it. do have video on how to convert number no c# database to string and display no windows form ? other question, do you also have video on how to display graph from database in c# using SQL Server
ty a lot. Would like to know how to allow only 2-digits after the decimal point.
but it was really helpfull. ^^
Thanks A lot man !!!
That very helpful, thanks very much.
That's really helpful. Thank you so much.
thanks! it helped me alot!
Make sure that you use the KeyPress event and not the KeyDown event.
Thanks for the easy solution ;)
--> You should use CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0] instead of checking for a dot-char, because in other cultures e.g. in german it will be the ',' char for commas
alternative:
private void breiteQuer_KeyPress(object sender, KeyPressEventArgs e)
{
double result;
char c = (char)e.KeyChar;
string text = tb.Text + c;
if (c != (char)Keys.Back && !double.TryParse(text, out result))
{
e.Handled = true;
}
}
simple and nice, thanks for your grateful help... cheers
Thanks for a simple solution.
Thanks a lot from France
If Textbox have . yet your example permit put other .
Thanks bro! it was helpful to me
Great explanation thanks
It's Very Helpful. Thank You
Thanks 😀
Great! Thanx. Really Helpful!
Wonderful, thank you for sharing this.
Thanks, Fine -!!! easy solution for hard problem
Get the ASCII code of '-' and add it to the condition!
Thanks a lot, it's helpfull :thumbup:
Thank you for sharing you knwlege
Great video.
it helped. Thanks
Amazing.. Thanks a lot
totally cool !
Many thanks!
Muito bom, bem aproveitado.
thank you
Thank you so much
Awesome!
very nice! thx!! saved me an hour
Спасибо большое))
Many thanks
Thanks Bro...
Excelent!
excellent...
Thanks!
thanks bro!
thank you dude
Thanks, Bro!!!...Sooo..Simle
thanks man =)
Thanks! =)
excellent
i need it n.n
thanks man
Awesome (y)