Hi group,

I recognize the today, most people navigate from control to control within a form with their mouse. However some old school people may want to use a "enter" key or a up, down, left or right arrow. I'm curious to know if these kinds of events (if that's the right word to describe the use of these keys) can realistically used in Visual Basic.

As an example, I (or the user) can use the tab key to move from one box to the next. But I have no way of going back one box (or two or three....) except by using the mouse to point and click. What options are available within VB to do such? Is this something that can be set in the properties on the controls? Is this a "keydown" event that I'll need to write code for?

I'm interested in your thoughts and ideas as well as ways to to easily accomplish (if such exists) using these various keys to navigate with.

In advance, thanks for helping this newbie!

Don

You can use shift-Tab to tab backwards through the controls.

The controls have a tabindex property. with this you can regulate what order the tab/shift-tab navigate through the controls. There's also a tabstop property to regulate which controls can be tabbed to.

Rev. Jim, I never knew you could shift-tab to go backwards. Thanks for the tip!!

Tinstaafl, I knew that the controls have tab index properties for tabbing. I've used that in every module I've created.

But I've wondered if there was someway I could use the "Enter" key as a tab or the up, down, left and right arrow keys. Can something like that be done?

Thanks guys. I appreciate the help.

Don

I'm not suggesting this as the best way. I only offer it as one way. Set the KeyPreview property of the form to True then add the following handler

Private Sub Form1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

    If Asc(e.KeyChar) = 13 And Me.ActiveControl.GetType() = GetType(TextBox) Then
        SendKeys.Send("{TAB}")
        e.Handled = True
    End If

End Sub

If you press the ENTER key while you are in any textbox, the ENTER key effectively gets translated into a TAB key. Actually what happens is the ENTER key gets ignored and the app gets sent a TAB key. Same difference. You can take this example and modify it with various conditions. I'm sure there is a better solution but this should work until one comes along.

Rev. Jim,

Thanks. I'm going to play with this a bit and see what and how it can work. I'm trying to think about how the end users will navigate. I know some will use the tab. Some will use the mouse. But it seems that 'Enter' seems to be the most used way to navigate.

Don

For multiple keys you can use the Or operator.

Private Sub txtLoginName_KeyDown(sender As Object, e As KeyEventArgs) Handles txtLoginName.KeyDown
    ' If the user presses the left arrow or the enter key.
    If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Enter Then
        ' navigate to the next controls text box.
        MyNextControl.MyNextControlsTextBox.Focus()
    End If
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.