Going to Next Control by Clicking Enter or Down Key in C# Windows Forms Application

Introduction

In this article I am describing some tricks that are very useful in Windows Form Application Development, as this minimizes the use of mouse and maximizes the functionality of your application.

If you want to go next control (textbox, comboBox, dateTimePicker..etc) you can press Enter Key or Down Key and if you want go to previous control you can press Up Key. If the control is comboBox or dateTimePicker we can use Control Key plus Up Key to go to previous Control and use only Enter Key to go to next Control.

Step 1

Create a Windows Form Application,

create

Step 2

Create a Form with some text boxes and one ComboBox and One DateTimePicker.

form

Step 3

Set all Controls TabIndex Property as in order. You can set this very easily. Follow the below steps

Open the Forms designer window and go to View menu and select Tab Order tool and set the TabIndex.

Step 4

Change KeyPreview property of the Form to true. This is very important one. As if this property of the form is false, this function will not work.

function

Step 5

Go to Key Down Event Handler of first textbox and Double click.

textbox

Step 6

Once again go to same event handler. Now you can see a name have given automatically. You can change the name as you wish. Please note that this name we use for all controls as common.

Here this is like(textBox1_KeyDown_1) I am changing this to (textBox_KeyDown)

Step 7

Create a Global Control Variable . Here I am Creating following one.

Control ctrl;

Step 8

Coding

Please put following code under the event handler.

Please note that the code to go to next control is

this.SelectNextControl(ctrl, true, true, true, true);

and code to go previous control is

this.SelectNextControl(ctrl, false, true, true, true);

code

Last Step

Go to Design window and copy the Keydown Event Handler name here, this is as follows

textBox_KeyDown

And copy the same in Keydown Event Handler of all Controlls.