Wednesday, July 30, 2008

Enter Key to Set Focus in Next Control (for Data Entry Forms)

Level: Beginner

Knowledge Required:
Windows Forms

In a Windows Application, we use Tab key to move Focus from current control to next control, but in a Data Entry applications, usually Enter Key is used for this purpose.

There are different techniques to accomplish this. I think the following is the simplest one.

First set the Form's KeyPreview property to True. Then put the following code in the KeyPress Event Handler:

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    If e.KeyChar = Chr(Keys.Return) Then
        Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
    End If
End Sub

1 comment:

Anonymous said...

Very helpful for windows application, however i was wondering if this is applicable in web.