Using Enter and Tab Keys to Navigate on a Form

When building Windows Forms application you may want to use a tab or return key to navigate on your textboxes. This code shows you how to do it.


When building Windows Forms application you may want to use a tab or return key to navigate on your textboxes.This code shows how to do it.

First register your keyDown event to your object KeyEventHandler. Like this example for a textbox calls IdTbx. Localize the property setting of the IdTbx then add the KeyDown event.

this.IdTbx.Location = new System.Drawing.Point(72, 32);
this.IdTbx.Name = "IdTbx";
this.IdTbx.TabIndex = 1;
this.IdTbx.Text = "";
this.IdTbx.KeyDown += new System.Windows.Forms.KeyEventHandler(this.IdTbx_KeyDown);
After implement the action IdTbx_KeyDown
as
private
void IdTbx_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if((e.KeyCode==Keys.Enter)||(e.KeyCode==Keys.Tab))
{
if (!IdTbx.Text.Equals(""))
{
PsswordTbx.Focus();
}
else
{
CancelBtn.Focus();
}
}
}