[英]Textbox validation in C# Winforms - Should allow only numerics between 1-100
Hello Sorry its a simple question, but would appreciate if someone can guide me with the code. 您好,抱歉,这是一个简单的问题,但是如果有人可以指导我使用代码,我们将不胜感激。 I have say 50 textboxes in my Winform.
我在Winform中说了50个文本框。 All should allow only numeric values and that too between 1-100 only.
全部应仅允许数字值,并且也只能在1-100之间。 How should I ensure this validation?
我应该如何确保此验证?
My thoughts was using e.Keychar in keypress event using Ascii values to restrict users to type only numerics. 我的想法是在使用Ascii值的按键事件中使用e.Keychar来限制用户仅键入数字。 Also, probably I can ensure this validation in Set in the property?
另外,也许我可以确保在属性的Set中进行此验证? But I don't know if I am correct and also don't know right code.
但是我不知道我是否正确,也不知道正确的代码。 Please help me.
请帮我。
Use NumericUpDown
instead of normal TextBox
box with validation. 使用
NumericUpDown
代替带有验证的普通TextBox
框。
A NumericUpDown control contains a single numeric value that can be incremented or decremented by clicking the up or down buttons of the control.
NumericUpDown控件包含一个数字值,可以通过单击该控件的上或下按钮来增加或减少。 The user can also enter in a value, unless the ReadOnly property is set to true.
除非将ReadOnly属性设置为true,否则用户也可以输入一个值。
You can specify the minimum and maximum numbers, it will allow the user to enter numbers between 1 and 100 and also let them use up and down buttons. 您可以指定最小和最大数字,这将允许用户输入1到100之间的数字,并允许他们使用向上和向下按钮。
EDIT: If you want to do it through code then you can try something like in KeyPress
event of your TextBox
: 编辑:如果您想通过代码做到这一点,则可以尝试在
TextBox
KeyPress
事件中进行类似操作:
private void yourTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
The above can be improved to access .
以上可以改善访问
.
for decimal numbers, but I guess you got the idea. 十进制数字,但我想你明白了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.