private void txtBSID_KeyPress(object sender, KeyPressEventArgs e)
{
// 限制输入数据位数,且不能为0
// 阻止从键盘输入键
e.Handled = true;
// 48代表0,57代表9,8代表退格,46代表小数点
if (e.KeyChar == 8)
{
e.Handled = false;
return;
}
if ((e.KeyChar < 48) || (e.KeyChar > 57))
{
return;
}
// 4-代表最多允许四位数字
if ((txtBSID.Text.Length >= 4) && (0 == txtBSID.SelectedText.Length))
{
return;
}
if ((0 == txtBSID.SelectionStart) && (48 == e.KeyChar))
{
return;
}
if (txtBSID.Text.Trim().Length == 0)
{
if (e.KeyChar != 48)
{
e.Handled = false;
}
}
else
{
e.Handled = false;
}
}
C# Winform数字输入框验证
最新推荐文章于 2024-03-18 22:06:14 发布