简体   繁体   English

在屏幕键盘上使用正则表达式

[英]Using regex on a on-screen keyboard

Ok i'm really getting stuck here guys :/. 好吧,我真的被卡在这里了。 I've got a (custom!) onscreen keyboard for which I wanna use regular expressions. 我有一个(自定义!)屏幕键盘,我想使用正则表达式。

I Successfully created a regex to only allow 0-9 and a comma for a regular keyboard. 我成功创建了一个正则表达式,只允许0-9和一个常规键盘的comma

Of course this isn't working for my on-screen keyboard. 当然,这不适用于我的屏幕键盘。

I have a code snippet that works... 我有一个有效的代码段...

BUT I also have a space button, a backspace button, and 2 buttons to move the caret around. 但是我也有一个空格键,一个退格键和2个用于移动插入符号的按钮。

When I use the below regex code and click the space, backspace or the caret buttons I get this error: 当我使用下面的正则表达式代码并单击空格,退格或插入符号按钮时,出现此错误:

Object reference not set to an instance of an object.

What am I doing wrong? 我究竟做错了什么?

    // the regex code   
    private Control keyb = null;
    private void EditProdPriceEx_GotFocus(object sender, RoutedEventArgs e)
    {
        string AllowedChars = "[^0-9,]";
        if (Regex.IsMatch(EditProdPriceEx.Text, AllowedChars))
        {
            keyb = (Control)sender;
        }
    }

Space button: 空格键:

    private void KnopSpatie_Click(object sender, RoutedEventArgs e)
    {
        TextBox tb = keyb as TextBox;
        int selStart = tb.SelectionStart; // here is where i get the error
        string before = tb.Text.Substring(0, selStart);
        string after = tb.Text.Substring(before.Length);
        tb.Text = string.Concat(before, " ", after);
        tb.SelectionStart = before.Length + 1;
    }

Backspace Button: 退格按钮:

    private void KnopWissen_Click(object sender, RoutedEventArgs e)
    {
        TextBox tb = keyb as TextBox;
        int cursorPosition = tb.SelectionStart; // here is where i get the error
        if (cursorPosition > 0)
        {
            tb.Text = tb.Text.Substring(0, cursorPosition -1) + tb.Text.Substring(cursorPosition);
            tb.SelectionStart = cursorPosition - 1;
        }
    }

Move caret left button: 向左移动插入符号:

    private void KnopLinks_Click(object sender, RoutedEventArgs e)
    {
        TextBox tb = keyb as TextBox;
        if (tb.SelectionStart > 0) // here is where i get the error
        {
            int selStart = tb.SelectionStart;
            string before = tb.Text.Substring(0, selStart);
            string after = tb.Text.Substring(before.Length);
            tb.SelectionStart = before.Length - 1;
        }
    }

Move caret right button: 向右移动插入符号:

    private void KnopRechts_Click(object sender, RoutedEventArgs e)
    {
        TextBox tb = keyb as TextBox;

        if (tb.SelectionStart >= 0) // here is where i get the error
        {
            int selStart = tb.SelectionStart;
            string before = tb.Text.Substring(0, selStart);
            string after = tb.Text.Substring(before.Length);
            tb.SelectionStart = before.Length + 1;
        }
    }

I think you must try this. 我认为您必须尝试一下。 In the first Code. 在第一个代码中。

// the regex code   
private Control keyb = null;
private void EditProdPriceEx_GotFocus(object sender, RoutedEventArgs e)
{
    string AllowedChars = "[^0-9,]";
    if (Regex.IsMatch(EditProdPriceEx.Text, AllowedChars))
    {
        keyb = (Control)sender;
    }
}

You made keyb null, and is not set by any key you mention (space and others), I think this produce the reference not set error. 您将keyb设为null,并且没有被您提到的任何键(空格和其他键)设置,我认为这会产生引用未设置的错误。 Can you check that?. 你能检查一下吗?

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

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