两种选择:
使用NumericUpDown
代替。NumericUpDown为您进行过滤,这很好。当然,它还使您的用户能够按键盘上的向上和向下箭头来增加和减少当前值。
处理适当的键盘事件,以防止除数字输入之外的任何操作。我已经在标准Text@R_441_2419@上成功使用了这两个事件处理程序:
private void text@R_441_2419@1_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 Text@R_441_2419@).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
如果您的Text@R_441_2419@不允许小数位,则可以删除该检查'.'
(以及随后的多个检查'.'
)。您还可以添加一个检查,以'-'
确定您的Text@R_441_2419@是否应允许负值。
如果要限制用户的位数,请使用: text@R_441_2419@1.MaxLength = 2; // this will allow the user to enter only 2 digits