您只能输入数字并使用箭头键和退格键。如果您输入的数字大于 100 或小于 1,当您按下回车键时,它将被取消。按下按钮复制和粘贴被禁用,并且鼠标右键单击以防止用户在文本框中粘贴被禁用/处理。这应该可以完全解决您的问题。首先设置:
ShortcutsEnabled property of your text box to False
这将不允许鼠标右键单击和 ctrl+V 粘贴到您的文本框中。然后添加以下代码:
//prevent letters, special chars, punctuation, symbols, white spaces
private void txtType1_KeyPress(object sender, KeyPressEventArgs e)
{
{
if (char.IsLetter(e.KeyChar) ||
char.IsSymbol(e.KeyChar) ||
char.IsWhiteSpace(e.KeyChar) ||
char.IsPunctuation(e.KeyChar))
e.Handled = true;
}
{
//allows only numbers between 1 and 100
string value = txtType1.Text;
if (txtType1.Text !="")
{
if (Int16.Parse(value) < 1 )
{
txtType1.Text = "";
}
else if (Int16.Parse(value) > 100)
{
txtType1.Text = "";
}
}
}
}