[C#] A TextBox that only accepts decimal/floating/double numbers
Vložit
- čas přidán 25. 09. 2012
- Our new facebook page : / 146893872037989
Hey guys, in this video I'll show you how to make a TextBox only accepts decimal/floating/double numbers in C#.
Make requests for our upcoming videos!
this is what I was looking for, thanks
Best answer I have found so far. Good job
THANK YOU for a really simple solution that I have been looking for to validate my floating point text box.
Amazing solution to capture prices, thanks dude
Wonderful, thank you for sharing this.
Thanks bro! it was helpful to me
simple and nice, thanks for your grateful help... cheers
That's really helpful. Thank you so much.
Thanks A lot man !!!
Great! Thanx. Really Helpful!
Thanks for a simple solution.
It's Very Helpful. Thank You
That very helpful, thanks very much.
thanks! it helped me alot!
ty a lot. Would like to know how to allow only 2-digits after the decimal point.
but it was really helpfull. ^^
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;
}
}
Muito bom, bem aproveitado.
Great explanation thanks
Amazing.. Thanks a lot
Nice video! Can you tell me how to add a comma to the filter?
Great video.
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;
}
Thanks, Fine -!!! easy solution for hard problem
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?
Thanks a lot from France
how to limit decimal place to 2 only while keypress?
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
totally cool !
Awesome!
Thank you so much
thanks bro!
Excelent!
Many thanks!
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.
Make sure that you use the KeyPress event and not the KeyDown event.
thanks man =)
Thanks a lot, it's helpfull :thumbup:
thank you dude
Thanks Bro...
How can i implement you code in WPF C#? Thank you so much if you can help....
Thank you for sharing you knwlege
it helped. Thanks
Thanks!
Thanks! =)
Get the ASCII code of '-' and add it to the condition!
Thanks 😀
Many thanks
excellent...
Thanks, Bro!!!...Sooo..Simle
thanks man
Спасибо большое))
Awesome (y)
very nice! thx!! saved me an hour
excellent
thank you
HOW I MAKE A TEXTBOX THAT ACCEPT ONLY EMAIL?
my e.KeyChar doesnt work :S how can i fix that , it gives a error and evrything is right
If Textbox have . yet your example permit put other .
i need it n.n