c# textbox enter press | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

c# textbox enter press

private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { textBox2.Focus(); } } // I want it to switch from textbox1 to textbox2 when I press the "enter" key in this code. When I click enter, it switches to other textbox (textbox 2), but the problem is that when I switch to other textbox it deletes all the items in textbox1. I want it not to be deleted. Please help me. Respects.

24th Feb 2020, 8:41 PM
Dagbfatih
Dagbfatih - avatar
9 Answers
+ 3
Can you add e.Handled = true after textBox2.Focus It seems the event is eating the characters. https://stackoverflow.com/questions/35413012/enter-key-clears-content-of-textbox (The thread is about VB.Net but this is similar in C#). or use SendKeys("{TAB}") In this way you convert the "enter"-key to a TAB-key. The TAB-key is general the key to move to the next control. Advantage of doing this is that you can use taborder to decide which controls should get focus next. https://www.youtube.com/watch?v=37exrtWvx4o
24th Feb 2020, 9:35 PM
sneeze
sneeze - avatar
+ 2
Don't worked I tried both ways but not happening again. Please help me!!
25th Feb 2020, 4:20 PM
Dagbfatih
Dagbfatih - avatar
+ 2
Please explain what happend or did not happen. I tried you inital code, even that code works at my laptop. I have a application with 2 textboxes (right from the toolbox). Type "hello" in textbox1, press "enter"-key Focus goes to textbox2. Can you create a test application, that can do just this ? Are you using winforms ?
25th Feb 2020, 8:52 PM
sneeze
sneeze - avatar
+ 1
Very thanks bro. I am out, I will try when I come. 👍👍
25th Feb 2020, 4:19 AM
Dagbfatih
Dagbfatih - avatar
+ 1
use imgur
27th Feb 2020, 9:14 PM
sneeze
sneeze - avatar
0
Please send to me your e-mail. I will send my video about this code to your e-mail. And I will send my code. Pls
26th Feb 2020, 1:17 PM
Dagbfatih
Dagbfatih - avatar
0
Sorry I don not send my e-mail adres to anyone. I you want to share do it via a public channel
26th Feb 2020, 8:33 PM
sneeze
sneeze - avatar
0
How?
26th Feb 2020, 8:48 PM
Dagbfatih
Dagbfatih - avatar
0
Can you open a little? sneeze
28th Feb 2020, 4:19 AM
Dagbfatih
Dagbfatih - avatar