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.
- I see a special symbol when I hold the shift key
- Pressing the backspace adds a new symbol to the line.
- 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.
- Back key doesn't erase a new line ("\n\r");
- 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.
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.
Or even better, use regex pattern.
solution from Only allow specific characters in textbox
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.
The idea is something that user3649914 mentioned, about t
Why don't you simply copy the text from the first text box to the second: