您应该在您想要此功能的每个文本框上设置此项。
它只是检查Text文本框是否为数字,然后检查范围并应用适当的值
yourtextbox.TextChanged+= (s, e) =>
{
var textbox = s as TextBox;
int value;
if (int.TryParse(textbox.Text, out value))
{
if (value > 255)
textbox.Text = "255";
else if (value < 0)
textbox.Text = "0";
}
}
Run Code Online (Sandbox Code Playgroud)