Pages

Subscribe:

Ads 468x60px

Tuesday, July 27, 2010

How to Move Cursor from One Page to Another Page Using VB.NET?

Today I am describing about moving cursor from one text box to another. When working VB.net it is very important to work with keyboard. Everyone want to avoid the use of mouse because there remains miscombination between putting input to forms and clicking mouse to switch from one box to another.
So you can easily do that using keyboard only. To do this, you have to write a very simple code.

You can switch cursor from one textbox to another, by pressing Enter button or by pressing Tab or by pressing any other key of the keyboard. For every cases, just one line will be changed among the entire code. So now lets go to start our work.

I present an example here for you to understand easily. Suppose, there is a form where I have two textbox under Name and City. Let, the cursor is in the textbox under Name. It appears like this:


I write in the Name field a sample name like Arif and then click Enter. Then the output must be like this i.e. the present cursor will be switched to textbox under City. It appears like this:


Now what about the code? If you choice to click Enter then the following code will be needed to be added under KeyDown Event:

Private Sub txtName_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtName.KeyDown
        If e.KeyCode = Keys.Enter Then
            txtCity.Focus()
        End If
End Sub

Here txtName is the name of the first textbox and txtCity is the name of the second textbox.

If you want to use the key F1 in lieu of Enter, you have to just place F1 for Enter. It will be like this:


Private Sub txtName_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtName.KeyDown
        If e.KeyCode = Keys.F1 Then
            txtCity.Focus()
        End If
End Sub

0 comments:

Post a Comment