TextBox只允许输入数字

方法一(来自罗友军)

 public static bool IsNumeric(string itemValue)
{
    return (IsRegEx("^(-?[0-9]*[.]*[0-9]{0,3})$", itemValue));
}

方法二(来自博为药库)

private void CellEdit_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (this.dgvDrugList.CurrentCell.ColumnIndex == 5 || this.dgvDrugList.CurrentCell.ColumnIndex == 7)
            {
                //若按下的是非数字,则不进行处理;  46 = '.'
                if (!(Char.IsNumber(e.KeyChar)) && !(e.KeyChar == (char)Keys.Enter) && !(e.KeyChar == (char)Keys.Left) &&
                    !(e.KeyChar == (char)Keys.Right) && !(e.KeyChar == (char)Keys.Back) && !(e.KeyChar == 46))
                {
                    e.Handled = true;
                }

                //当存在小数点时,若再输入小数点将不会进行处理
                string str = ((TextBox)sender).Text;
                if (str.Contains(".") && (e.KeyChar == 46))
                    e.Handled = true;
            }
        }

相关文章:

  • 2021-06-07
  • 2021-08-10
  • 2022-12-23
  • 2022-01-13
  • 2021-10-18
猜你喜欢
  • 2021-10-18
  • 2021-12-17
  • 2021-08-08
  • 2021-10-18
  • 2022-12-23
相关资源
相似解决方案