Results 1 to 11 of 11

Thread: [RESOLVED] [2008] Move To next TextBox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2005
    Location
    Bulawayo, Zimbabwe
    Posts
    576

    Resolved [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?

  2. #2
    Junior Member
    Join Date
    Oct 2008
    Posts
    26

    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?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2005
    Location
    Bulawayo, Zimbabwe
    Posts
    576

    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.

  4. #4
    Junior Member
    Join Date
    Oct 2008
    Posts
    26

    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:
    1. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    2.         If e.KeyCode = Keys.Enter Then SendKeys.Send("{tab]")
    3.     End Sub

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Sub TextBox3_KeyDown(ByVal sender As Object, _
    2.                              ByVal e As KeyEventArgs) Handles TextBox3.KeyDown, _
    3.                                                               TextBox2.KeyDown, _
    4.                                                               TextBox1.KeyDown
    5.     If e.KeyCode = Keys.Enter Then
    6.         Me.SelectNextControl(Me.ActiveControl, _
    7.                              Not e.Shift, _
    8.                              True, _
    9.                              True, _
    10.                              True)
    11.     End If
    12. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     Me.KeyPreview = True
    3. End Sub
    4. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    5.     If e.KeyCode = Keys.Enter AndAlso TypeOf Me.ActiveControl Is TextBox Then
    6.         Me.SelectNextControl(Me.ActiveControl, _
    7.                              Not e.Shift, _
    8.                              True, _
    9.                              True, _
    10.                              True)
    11.     End If
    12. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2005
    Location
    Bulawayo, Zimbabwe
    Posts
    576

    Re: [2008] Move To next TextBox

    Thanks Guys
    My question was answered and I also learned a few other things in the process

  11. #11
    Addicted Member
    Join Date
    May 2002
    Posts
    142

    Re: [RESOLVED] [2008] Move To next TextBox

    Thanks! This helped me !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width