简体   繁体   English

文本框仅接受数值

[英]text box to accept the numerical values only

the user should be allowed to enter only decimal values in the text box 应该允许用户在文本框中仅输入十进制值

for this keydown event is listened and the values are compared the regular expression 侦听此keydown事件并将其值与正则表达式进行比较

@"^((\+|-)?(\d*))+((\.|,)\d{0,5})?$";

the intention here is to restrict the decimal places to 6. 目的是将小数位限制为6。

, is used as a decimal seperator for the european languages ,用作欧洲语言的十进制分隔符

the format of the numbers can be 数字的格式可以是

+100, 100
-100
.12
10.12 .....

if the expression entered in the fashion works fine 如果以时尚形式输入的表达式效果很好

suppose the entered values on the text box 100.123456 now if want to modify the above value like below 如果想像下面这样修改上面的值,现在假设在文本框100.123456中输入的值

1100.123456
1001.123456

i am not able to modify because the Regex.IsMatch returns false, if the regex.ismatch returns false i am suppressing the key events 我无法修改,因为Regex.IsMatch返回false,如果regex.ismatch返回false,则我抑制了按键事件

why regex.ismatch returns false when the 100.123456 is modified to 1100.123456 do i need to modify the regular expression 为什么将100.123456修改为1100.123456时regex.ismatch返回false,我是否需要修改正则表达式

您不能简单地设置文本框的属性或属性以使其仅接受数字输入吗?

I wouldn't match against a regex, just do Double.TryParse and if it parses correctly use that number. 我不会与正则表达式匹配,只需执行Double.TryParse ,如果解析正确,则使用该数字。

Is there a reason you need it to match a RegEx exactly? 您是否有理由与正则表达式完全匹配?

If you want to restrict the numbers after the decimal point to 6, shouldn't you be using the Regex 如果要将小数点后的数字限制为6,则不应该使用Regex

^((\+|-)?(\d*))+((\.|,)\d{0,6})?$

Note the 5 from the original is replaced by a 6. The values you provided are not matching your regular expression because they have 6 digits after the decimal point, whereas your Regex expects between 0 and 5 inclusive. 请注意,原始的5被替换为6。您提供的值与您的正则表达式不匹配,因为它们的小数点后有6位数字,而Regex期望介于0和5之间。

Let me preface this answer by saying: this is the sort of thing that is probably best handled by a 3rd-party control. 让我以这样的方式开头这个答案:这是最好由第三方控件处理的事情。 That way the solution completely handles the corner cases and the necessary testing. 这样,该解决方案可以完全处理极端情况和必要的测试。

The KeyDown event is going to fire before the text in the control changes. 在控件中的文本更改之前,将触发KeyDown事件。 If you are comparing the textbox .Text value in your regex it isn't going to have the new value. 如果要在正则表达式中比较textbox .Text值,则不会有新值。

You could try generating the string that would be created if the key is allowed, and then run the regex against it. 您可以尝试生成如果允许使用密钥将创建的字符串,然后对它运行正则表达式。

(With props to C# How to translate virtual keycode to char? ) (使用C#的道具如何将虚拟键码转换为char?

[DllImport("user32.dll")]
static extern int MapVirtualKey(uint uCode, uint uMapType);

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    string number = textBox1.Text;

    if ((e.KeyData & Keys.Delete) == Keys.Delete)
    {

    }
    else if ((e.KeyData & Keys.Back) == Keys.Back)
    {

    }
    else
    {
        // 2 is used to translate into an unshifted character value 
        int nonVirtualKey = MapVirtualKey((uint)e.KeyData, 2);
        char mappedChar = Convert.ToChar(nonVirtualKey);

        string proposedString = number.Remove(textBox1.SelectionStart, textBox1.SelectionLength).Insert(textBox1.SelectionStart, mappedChar.ToString());

        bool works = Regex.IsMatch(proposedString, @"^((\+|-)?(\d*))+((\.|,)\d{0,5})?$");

        e.SuppressKeyPress = !works;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM