在 C# 和 Windows 窗体应用程序中,
我有一些文本框,我想强制用户只能输入数字和特定数字,例如在本例中我的数字范围是 {2.1, 2.2, ..., 9.4, 9.5}
这就是我强制文本框只接受数字的方法:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!((int)e.KeyChar>=48 && (int)e.KeyChar<= 57 || (int)e.KeyChar ==8))
{
e.KeyChar = (char)27;
}
}
但我不知道如何为其设置范围:例如仅在 {2.1 到 9.5} 或 {25 到 91} 之间
最佳答案
对于 WinForms,只需使用 NumericUpDown .
将其拖放到表单上,根据您的喜好设置 Minimum
、Maximum
、DecimalPlaces
和 Increment
参数工作就完成了。
通过控件的 .Value
访问该值属性(property)。
关于c# - 如何强制文本框只接受 2.1 到 9.5 之间的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36136975/