简体   繁体   中英

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

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

the intention here is to restrict the decimal places to 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

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

why regex.ismatch returns false when the 100.123456 is modified to 1100.123456 do i need to modify the regular expression

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

I wouldn't match against a regex, just do Double.TryParse and if it parses correctly use that number.

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

^((\+|-)?(\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.

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. If you are comparing the textbox .Text value in your regex it isn't going to have the new value.

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? )

[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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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