键入键后操作按键方法

时间:2021-12-05 00:05:53

I'm actioning a method on a text box's KeyPress event, but the method is being run before the key is actually typed into the box.

我正在对文本框的KeyPress事件执行一个方法,但是在将键实际输入到框中之前,该方法正在运行。

If I use KeyUp, I get the behaviour I'm looking for, except KeyUp triggers on soft keys as well as standard characters, which isn't particularly useful for me.

如果我使用KeyUp,我会得到我正在寻找的行为,除了软键上的KeyUp触发器以及标准字符,这对我来说并不是特别有用。

Can I get KeyPress to run after the key is typed into the box? Or can I easily get KeyUp to ignore soft keys?

在键入框中后,我可以让KeyPress运行吗?或者我可以轻松地让KeyUp忽略软键吗?

4 个解决方案

#1


Well, you can remember keychar in KeyPress event, and do all necessary logic in KeyUp, using remembered char.

好吧,你可以记住KeyPress事件中的keychar,并使用记忆的char在KeyUp中完成所有必要的逻辑。

#2


In your keypress event, you can concatenate textbox.Text and e.KeyChar into a new string and process this new string.

在keypress事件中,您可以将textbox.Text和e.KeyChar连接成一个新字符串并处理这个新字符串。

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{    
    string newString = textBox1.Text + e.KeyChar.ToString();
    // Process newString
    // If after processing you do not wish to undo the typed letter do 
    // e.Handled = true;
}

#3


Use keyUp with as code:

使用keyUp作为代码:

private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
    //only do things when an alphabetical character is typed
    if ((e.KeyCode >= Keys.a) && (e.KeyCode <= key.z) ||
        (e.KeyCode >= Keys.A) && (e.KeyCode <= key.Z))
    {
        // do your thing
    }
}

You can ofcourse also check on other characters like . etc.

您还可以检查其他字符。等等

#4


You can also try the TextChanged event from your textbox control.

您还可以从文本框控件中尝试TextChanged事件。

#1


Well, you can remember keychar in KeyPress event, and do all necessary logic in KeyUp, using remembered char.

好吧,你可以记住KeyPress事件中的keychar,并使用记忆的char在KeyUp中完成所有必要的逻辑。

#2


In your keypress event, you can concatenate textbox.Text and e.KeyChar into a new string and process this new string.

在keypress事件中,您可以将textbox.Text和e.KeyChar连接成一个新字符串并处理这个新字符串。

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{    
    string newString = textBox1.Text + e.KeyChar.ToString();
    // Process newString
    // If after processing you do not wish to undo the typed letter do 
    // e.Handled = true;
}

#3


Use keyUp with as code:

使用keyUp作为代码:

private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
    //only do things when an alphabetical character is typed
    if ((e.KeyCode >= Keys.a) && (e.KeyCode <= key.z) ||
        (e.KeyCode >= Keys.A) && (e.KeyCode <= key.Z))
    {
        // do your thing
    }
}

You can ofcourse also check on other characters like . etc.

您还可以检查其他字符。等等

#4


You can also try the TextChanged event from your textbox control.

您还可以从文本框控件中尝试TextChanged事件。