samedi 1 août 2015

Unable to get desired output for keydown c#

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. This is the output screen for any clarifications

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?

Aucun commentaire:

Enregistrer un commentaire