Unable to get desired output for keydown c#

2019-08-06 08:54发布

问题:

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.

  1. I see a special symbol when I hold the shift key
  2. Pressing the backspace adds a new symbol to the line.
  3. Simultaneous pressing of the back key doesn't remove or add(the new symbol) more than once. I assumed the back key would erase 1 character each time it was pressed.
  4. Back key doesn't erase a new line ("\n\r");
  5. The last two lines are for the alt and ctrl keys respectively.

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?

Edit: 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.

回答1:

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.

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

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

solution from Only allow specific characters in textbox



回答2:

Why don't you simply copy the text from the first text box to the second:

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


回答3:

I've worked around it. It is along the lines of what user3649914 suggested.
The following has solved the issue but the order of if else conditions is very crucial in this case. Not sure if that's good programming practice to rely so heavily on the order of if else though.

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

The idea is something that user3649914 mentioned, about t