Followers

Thursday, March 03, 2005

Shifting focus on next control on pressing the Enter key

if you want to configure enter key to move to next control in .NET then use following technique.

Set KeyPreview property of the form to "True"

Trap Key_down event of form and check if enter key was pressed. if then pass the focus to next control. code looks like below.

private void Form1_KeyDown ( object sender, KeyEventArgs e )
{
if ( e.KeyCode == Keys.Return )
{
Control c = GetNextControl ( ActiveControl, true ) ;
c.Focus( ) ;
}
}

.The ActiveControl property returns the reference of the control that is currently selected. The GetNextControl( ) method returns the control next to the specified control in tab order. The focus is set on the next control using the Focus( ) method.

No comments: