There are couple of ways doing it...
Use Key_Press event as below on the textbox
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(! (char.IsDigit(e.KeyChar) || e.KeyChar == '.') ) //
use the keys that you want to handle
{
e.Handled = true;
}
}
and also textbox validation event as below
private void textBox1_Validated(object sender, EventArgs e)
{
double d;
if(double.TryParse(textBox1.Text,out d))
{
d = Math.Round(d,2);
textBox1.Text = d.ToString();
}
}
This should solve the problem.
However I would like to derive a class from Textbox and handle the
above events in the derived class and use the same on the form. This
will give you flexibility in the future.
-Cnu
On Jun 2, 1:27*am, "Ronald S. Cook" <rc...@westinis.comwrote:
Stupid q for the day, but if the user enters 2 in a textbox, I want to
display as 2.00.
If they enter 2.129 I want it as 2.13.
I'm guessing will code in the validated method of the textbox, but not sure
how to convert the number.
Thanks for the help.