Social Icons

Wednesday, November 21, 2012

Allowing only Decimal values in Text Box Csharp


You can handle this in keyPress event of Text Box as in following way....


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;
     }
}

No comments:

Post a Comment

 

Sample text

Sample Text