- You First Have To Go To The Properties Of The Textbox And Find For The Event Keypress And Double Click The Event Then You Will Get A New Event Like Below ..
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
}
- Now All You Have To Copy The Code Below And Paste It Inside The Event Function...
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
----------------------------------------------------------------------------------------------------------------------------------
// only allow one coma
if (e.KeyChar == ',' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
- And It Should Look Like This .. When Done..
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)&& e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
No comments:
Post a Comment