Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tab order 1

Status
Not open for further replies.

honeypot

Technical User
Mar 6, 2001
147
GB
hi
ive set up my tab order on my form which works fine but i'd like to be able to move thru the controls in the same way using the enter key. How do i set this up? I assumed that it would do it automatically!

 
Try:

Code:
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyValue = 13 Then ' 13 is the value for the enter key
            TextBox2.Focus()
        End If
    End Sub

- B
 
Brian is correct in what he says, but no way do you want to code every textbox, to handle the Enter Key.

What you need to do is create a component class of the Textbox, and include the code supplied by Brian in it. Then instead of using the Standard .Net Textbox, you use your derived Textbox.

All the joys of Inheritance...and OOP [shadeshappy]

Sweep
...if it works dont mess with it
 
Try this

I haven't tested this but this might work. Add this to your base form keydown event

Code:
        Dim oControls As Control
        Dim oNextControls As Control

        If (e.KeyCode = Keys.Enter) Then

            oControls = Me.ActiveControl
            oNextControls = Me.GetNextControl(oControls, True)
            oNextControls.Focus()
        End If

GetNextControl will loop thru your control collection in the order in which the taborder was set at design time. You might need to add more error checkings. The only problem would be with buttons or the controls that accepts "Enter" key. Because once your on a button pressing "Enter" will trigger the clicked event. So for that I think you might have to create a base button class and add keydown event there to trap that. I guess this might get you start on the project. Let me know if this works for you even I am curious to hear the result.

-Kris
 
Kris,

I think the button problem could be circumvented by not handling the KeyDown for the form, but with one event handler that handles each textbox's keydown.
 
Yes, that might be an easier approach but I think the question was not just looping thru textboxes, but any control same as the way how we use “TAB” key. I think still your suggestion might work, got to try it and see.

-Kris
 
Thanx for your suggestions :) i put the following code in and it now works:

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

If e.KeyChar = Microsoft.VisualBasic.Chr(13) Then
SendKeys.Send(vbTab)
End If

End Sub

note that the keypress on the form ,not on the textbox.

and be sure that the form property (KeyPreview) is True.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top