强制文本框仅具有整数值 [英] Forcing Text box to have only integer values

查看:68
本文介绍了强制文本框仅具有整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个只能具有整数值的文本框.
用户单击确定"按钮时,应检查文本框中的输入是否为整数值,如果不是整数值,则应显示仅输入整数值的消息.
并应将值重置为null.


我是这样做的.

I need a Text box that can only have integer values.
when user click the ok button, it should check whether the input in text box is an integer value, and if it isn''t an integer value then it should show the message to enter only integer value.
and should reset the value to null.


I have done it this way.

private void ok_Click(Object sender, EventAegs e)
{
   if(System.Text.regularExpressions.Regex.IsMatch("[^0-9]",exp_weight.Text))
    {
        ok=true;
        this.Hide();    
    }
    else{
    MessageBox.Show("Please Enter Number Only");
    exp_weight.Text=="";  
    } 
}



但这仅适用于数字0或9,对于除此以外的任何数字,它都会显示消息请仅输入数字".

现在有身体可以帮我吗?

谢谢



But this only work for the number 0 or 9, for any number other then this it shows the message "Please enter Number Only".

Now can some body help me??

Thanks

推荐答案

您使用的解决方案将取决于您希望如何进行检查.

如果要阻止用户输入非数字字符,则所有以前的解决方案都可以使用.

如果希望用户能够输入所需内容并在最后检查输入内容,则应调查TextBox.Validating事件.
The solution you use will depend on how you want the checking to be done.

If you want to prevent the user from being able to enter a non-numeric character then all of the previous solutions will work.

If you want the user to be able to enter what they want and check the input at the end then you should investigate the TextBox.Validating event.


不要使用TextBox !请改用NumericUpDown!瞧,问题会自动解决.
Don''t use a TextBox! Use a NumericUpDown instead! Voila, problem automatically solved.


Google或在CodeProject中搜索"屏蔽文本框"

您需要使用onKeyPress事件,然后可能使用常规的experession或ASCII值来验证按下的键并采取相应的措施.
Google or search CodeProject for ''Masked Textbox''

You need to use onKeyPress event and then probably use regular experession or ASCII values to verify the key pressed and act accordingly.


这篇关于强制文本框仅具有整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆