无法获取的keydown C#所需的输出所需、keydown

2023-09-06 10:20:53 作者:莪们说好的

我想创建一个使用Windows窗体两个同步文本框,但我似乎遇到了许多问题。也许你们可以帮我做的事情我一点更有效。

I am trying to create two synchronized textboxes using windows forms but I seem to be running into multiple problems. Maybe you guys can help me make things a little more efficient for me.

正如你所看到的,不是回车键的问题,因为我已经慎重了。

As you can see, there isn't a problem for the enter key, because I've taken care of that.

在我看到一个特殊的符号时,我按住shift键 pressing退格增加了一个新的符号就行了。 返回键不会删除或添加(新符号)不止一次的同时pressing。我以为返回键会删除它pressed每次1个字符。 返回键不删除一个新行(\ñ\ r); 在最后两行分别是中高音和Ctrl键。

在code为这种形式到目前为止

The code for this form so far is

private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        //Disable usage of arrowkeys
        if(e.KeyCode==Keys.Left || e.KeyCode==Keys.Right || e.KeyCode==Keys.Up || e.KeyCode==Keys.Down)
        {
            e.SuppressKeyPress=true;
        }

        //Remove a character
        if (e.KeyCode == Keys.Back)
            textBox2.Text = textBox2.Text.Remove(textBox2.TextLength - 1, 1);

        //Upper and lower case characters
        if (!e.Shift && e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z)
            textBox2.Text += (char)(e.KeyValue + 32);
        else
            textBox2.Text += (char)(e.KeyValue);

        //Next Line
        if (e.KeyData == Keys.Enter)
            textBox2.Text += "\n\r";
    }

你有什么建议我该怎么办?

What do you suggest I do?

编辑:这不是我的最终意图。我想每个输入转换成不同的重新presentation,是实时的。现在,我只是检查了每个输入可能意味着。

This isn't my ultimate intent. I wish to convert each input into a different representation, in real time. For now, I am just checking what each input could mean.

推荐答案

我同意M4N这将是更容易只是从一个文本框将文本复制到下一个。

I agree with M4N it would be much easier to just copy the text from one text box to the next.

不过,如果你在做这样的意图,还有很长的路绕过它。

However, if your intent on doing it this way, there is a long way around it.

每个键preSS将有一个值,你可以摆脱所有你不想使用的按键。

Each key press will have a value and you can escape all the keys you don't want to use.

下面是从MSDN网站上的链接值

Here is a link to the values from the MSDN website

https://msdn.microsoft.com /en-us/library/aa243025(v=vs.60).aspx

您都准备好使用改变字符转换为大写类似的方法。

Your all ready using a similar method for changing characters to uppercase.

If (e.KeyValue >= 65 && e.KeyValue <= 90 ){
 #check for spaces and return the enter add to other text box.
}

甚至更好,使用正则表达式。

Or even better, use regex pattern.

Regex regex = new Regex(@"[0-9A-Z\s]+");
MatchCollection matches = regex.Matches(textValue);

从解决方案只允许在特定的文本字符