Hi Techies,
I am making a windows form.
I am trying to validate a text box to accept only
float value. Below is the code, where txtPurchasePrice is my textBox.
The code below works fine:-
private void
txtPurchasePrice_TextChanged(object sender, EventArgs e)
{
txtPurchasePrice.KeyPress += new
KeyPressEventHandler(rtbQuantity_KeyPress);
}
private void rtbQuantity_KeyPress(object sender, KeyPressEventArgs e)
{
////if ((e.KeyChar >= '0') || (e.KeyChar <=
'9'))
//// e.Handled = true;
if
(!char.IsNumber(e.KeyChar) & (Keys)e.KeyChar != Keys.Back
& e.KeyChar != '.')
{
e.Handled =
true;
}
base.OnKeyPress(e);
}
Problem: It accepts only first letter as alphabet, rest is OK. (means not working on first entered letter)
Help Plzz.-------------------------------
Chandrabhan Pradhan
[email protected]
Wim SturkenboomPosted Sep 13, 2014, 3:12 AM
Code:
float f;
if (!float.TryParse(txtPurchasePrice.Text, f))
{
// not a valid float
}
Nilesh WaghPosted Sep 13, 2014, 2:01 AM
Hirendra SisodiyaPosted Apr 26, 2010, 1:17 AM
Why are you not using direct keypress event for txtPurchasePrice.
if you want to using common function you can try like this:
private void txtPurchasePrice_KeyPress(object sender, KeyPressEventArgs e)
{
Validate(e);
}
private void Validate(KeyPressEventArgs E)
{
if (!char.IsNumber(E.KeyChar) & (Keys)E.KeyChar != Keys.Back
& E.KeyChar != '.')
{
E.Handled = true;
}
}
thanks
Please mark as answere if it helps