C# - Press Enter Key to move to next control on a Windows Form using single code
1. In the Form, Select Form--> Press F4 to open the Property box. or
Click on View Menu--> Property Window menu item.
2. This will open the Property box.
3. Look for property KEY PREVIEW and set it to TRUE.
This will make the form listener to all events we are performing on Controls. This is useful to avoid writing Lost Focus() code for each control.
4. Now here is the little code you need to write for Forms KeyDown event.
//Code to be pasted in your form. My Form name is frmNewInput. Change your code as appropriate formname.
private void frmNewInput_KeyDown(object sender, KeyEventArgs e)
if (e.KeyCode == Keys.Enter)
this.SelectNextControl(this.ActiveControl, true, true, true, true);
Note: Please dont use that (Control)sender instead of this.ActiveControl as this wont work because it wont understand which control to focus now. As you must be aware that SelectNextControl(..) is a in-built into .NET framework 3.5 so there is no hassle or performance issues.
You can freely use my code in yours and make your creations well.
|Guest Author: Abhirup 19 Jun 2012|
|Really Excellent one. This helped me a lot.|
|Guest Author: bharat 29 Oct 2012|
|thanks sir, i need it and i like it but i want to previous so what can i do ?|
please response with firstname.lastname@example.org
|Author: Nikesh Kumar Sinha 30 Nov 2012||Member Level: Bronze Points : 3|
thanks for the comment. For the Previous, you need to make a small change :
change the first bool Parameter to false. It will look below :
this.SelectNextControl(this.ActiveControl, false, true, true, true);
I tested it and it works well. Hope it will work for you too. Write to me in case you have any issue.
|Guest Author: Nikhil Jain 14 Dec 2012|
its very useful
|Guest Author: Gopal 28 Dec 2012|