Tuesday, February 15, 2011

C# - Create Numeric Textbox


How to create numeric textbox in C#.

  private void txtBlockQty_KeyPress(object sender, KeyPressEventArgs e) 
{
           if ((e.KeyChar < '0') || (e.KeyChar > '9')) e.Handled = true; 
           if (e.KeyChar=='.') e.Handled = false;
}

How to validate text for numeric value in  C#.

# Create a function

private bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
{
          try         
         {
                   Double result;                   

                  return Double.TryParse(val, NumberStyle, System.Globalization.CultureInfo.CurrentCulture, out result);          
          }
          catch (Exception)                    

          {
                      throw;                   

                      return false;
          }
}

# Call it like this
if (!isNumeric(txtAnualRent.Text.Trim(), System.Globalization.NumberStyles.Currency)) 
{
          MessageBox.Show("Enter valid Anual Rent !!", "Errer !", MessageBoxButtons.OK, MessageBoxIcon.Hand);
          validation = false;
}

No comments:

Post a Comment