文本框应接受0到366之间的数字 [英] Textbox should accept numbers from 0 to 366
本文介绍了文本框应接受0到366之间的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一个文本框,它只接受0到366之间输入的数字.
没有其他任何事情...
I''v a textbox and it should accept only numbers entered from 0 to 366.
Not any other things...
推荐答案
您可以为validate事件添加以下委托:
You may add the following delegate for the validate event:
private void Validate_Text(object sender, CancelEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb != null)
{
int i;
if (int.TryParse(tb.Text, out i))
{
if (i >= 0 && i <= 366)
return;
}
}
MessageBox.Show("invalid input");
e.Cancel = true;
}
当然,您会为用户提供更好(更有用)的消息.
:)
Of course you will provide a better (more informative) message for the user.
:)
在文本框的按键事件中写入
at the keypress event of textbox write
if (!(e.KeyChar >= ''0'' && e.KeyChar <= ''9''))
e.Handled = true;
只能输入数字.
当我得到您的第二个问题时,有关文本框内数字的限制.为此,您可以使用textnox的 MaxLength 属性
it will secure to enter only numbers.
and as i getting your second question is about limitation of numbers enetered in Textbox. for this you can use MaxLength property of textnox
如何使用NumericUpdown Control [ ^ ]]?它可以满足您的需求.它具有最小值",最大值",跳过"属性,并接受一个数字.
How about you use NumericUpdown Control[http://msdn.microsoft.com/en-us/library/729xt55s.aspx[^]]? It does what you want. It has Minimum, Maximum, Skip properties, and takes in a number.
这篇关于文本框应接受0到366之间的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文