0

C# textbox with double value filter

Simply handle KeyPress event. Allow copy, paste, cut and backspace too.

private void txtSpadRef_KeyPress(object sender, KeyPressEventArgs e)
{
  Double isNumber = 0;
  String s = e.KeyChar.ToString();
  int[] allowedChar = new int[] {3,8,22,24 };
  foreach (int i in allowedChar) { 
    if(e.KeyChar.Equals(Convert.ToChar(i))){
      return;
    }
  }

  if (s == "." && txtSpadRef.Text.IndexOf(".") < 0)
  {
    s += "0";
  }
  e.Handled = !Double.TryParse(s, out isNumber);
}



 
Copyright © peyotest