1. 限制数字位数
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/43c0234ffb2204a4bd133bc2ae500c8a.png)
2. 限制只能输入数字,Textbox控件事件的“KeyPress”上双击
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
e.Handled = true;
}
3. 限制只能输入范围,Textbox控件事件的“TextChanged”上双击
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
textBox1.Text = 1.ToString();
}
int number = int.Parse(textBox1.Text);
if ((number >= 1) && (number <= 8))
{
}
else if(number < 1)
{
number = 1;
}
else if(number > 8)
{
number = 8;
}
textBox1.Text = number.ToString();
}
}