[RESOLVED] [2008] Move To next TextBox
I have a form with a number of text boxes on it
How do i get the program to move focus to the next text box when a user hits the enter key.
I used to use control arrays in VB6.
I know that you can trap the keydown event and if it is 13 or Keys.enter then move focus to the next text box, but this means that I have to have a Keydown event trap for every textbox on the form.
Isn't there an easier way?
Re: [2008] Move To next TextBox
Why not just setup the tabindex's inline for the textboxes and have the user hit the tab key to go through them?
Re: [2008] Move To next TextBox
Thanks
I have that in place already.
This is a request from the customer since the old version of the software offered this feature.
Re: [2008] Move To next TextBox
Quote:
Originally Posted by KenBZim
Thanks
I have that in place already.
This is a request from the customer since the old version of the software offered this feature.
How about this?
vb.net Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then SendKeys.Send("{tab]")
End Sub
Re: [2008] Move To next TextBox
You only need one event handler and you just put all the TextBoxes in the Handles clause. This is the very reason that control arrays don't exist in VB.NET as they did in VB6. Design-time support simply isn't needed because you can simply select all your TextBoxes in the designer, open the Properties window, click the Events button and double-click the KeyDown event. The IDE will generate an event handler and populate the Handles clause. You then just trap the Enter key and call SelectNextControl:
vb.net Code:
Private Sub TextBox3_KeyDown(ByVal sender As Object, _
ByVal e As KeyEventArgs) Handles TextBox3.KeyDown, _
TextBox2.KeyDown, _
TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
Me.SelectNextControl(Me.ActiveControl, _
Not e.Shift, _
True, _
True, _
True)
End If
End Sub
Note that the 'Not e.Shift' bit means that hitting Enter will move forward and hitting Shift+Enter will move back, just like Shift+Tab. If you don't want that behaviour then just put True there instead, so it will always go forward.
Re: [2008] Move To next TextBox
Enhanced version of jm code.
This won't require to wireup each textbox seperately. (especially good in cases where you have too many textboxes on the form and you want the feature on all the textboxes.)
vb.net Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter AndAlso TypeOf Me.ActiveControl Is TextBox Then
Me.SelectNextControl(Me.ActiveControl, _
Not e.Shift, _
True, _
True, _
True)
End If
End Sub
Pradeep :)
Re: [2008] Move To next TextBox
what if the 'next' control is not a textbox? the OP said "How do i get the program to move focus to the next text box when a user hits the enter key." the OP did not say what other controls where on the form.
Re: [2008] Move To next TextBox
Quote:
Originally Posted by dbasnett
what if the 'next' control is not a textbox? the OP said "How do i get the program to move focus to the next text box when a user hits the enter key." the OP did not say what other controls where on the form.
Put additional checks for that or set the tab index in the order you want it to go.
But I assume he wants it for a data entry form which usually has textboxes.
After the last textbox, it should set the focus to OK/Save button and when that button is clicked (enter pressed), it should save the record.
Pradeep
Re: [2008] Move To next TextBox
Quote:
Originally Posted by dbasnett
what if the 'next' control is not a textbox? the OP said "How do i get the program to move focus to the next text box when a user hits the enter key." the OP did not say what other controls where on the form.
It's extremely unlikely that you'd want a feature like this on a form where TextBoxes were interspersed with other controls and you wanted the Enter key to skip controls in the tab order. It's possible but unlikely. If that's what's actually required then this is an example of why you should post fully and clearly in the first place, so people don't make invalid assumptions. If that's what you wanted then you could use the method I suggested to create the event handler and then create your own array to store the TextBoxes, specifically selecting the next control in that array. The fact that control arrays are not supported by the VB.NET designer doesn't mean you can't create an array of controls in code.
Re: [2008] Move To next TextBox
Thanks Guys
My question was answered and I also learned a few other things in the process
Re: [RESOLVED] [2008] Move To next TextBox