Results 1 to 8 of 8

Thread: [2005] Accept Enter Key

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    [2005] Accept Enter Key

    I have 4 textbox in one form, If I press tab key it move to textbox1 to textbox2…
    I want to this will be done press Enter Key also , Is there any properties ?


    thanks

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: [2005] Accept Enter Key

    vb Code:
    1. If e.KeyCode = Keys.Escape Then
    2.             TextBox2.Focus()
    3. End If

    Something like that in the keydown event of each textbox could work. Make sure you change the TextBox number to the one you want to set focus to.

  3. #3
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: [2005] Accept Enter Key

    To achieve this, I add a handler for the Text Box KeyDown events in the Form Load event as follows. This will set the handler for all text boxes on the form so that you don't have to handle each control's KeyDown event individually.

    Paste this into your form and then simply set the TabStop properties of all text boxes to True and set the TabIndex properties in the order that you wish to tab.

    Code:
     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            AddHandlers()
        End Sub
    
        Private Sub ReturnKeyTabs(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
            ReturnKeyTabs(e.KeyCode)
        End Sub
    
        Private Sub ReturnKeyTabs(ByVal KeyCode As System.Windows.Forms.Keys)
    
            If KeyCode = System.Windows.Forms.Keys.Return Then
                System.Windows.Forms.SendKeys.Send("{Tab}")
                KeyCode = 0
            End If
    
        End Sub
    
        Private Sub AddHandlers()
            Try
    
                Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control in the tab order.
    
                Do Until ctl Is Nothing
    
                    If TypeOf ctl Is System.Windows.Forms.TextBox Then
    
                        AddHandler ctl.KeyDown, AddressOf ReturnKeyTabs
    
                    End If
    
                    ctl = Me.GetNextControl(ctl, True) 'Get the next control in the tab order.
    
                Loop
    
            Catch ex As Exception
    
            End Try
    
        End Sub

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2005] Accept Enter Key

    Typically, I use the KeyPress even for enter rather than KeyDown.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    Re: [2005] Accept Enter Key

    Code is working properly if only textbox is there , if i put button in between then it do not move to next textbox.
    ex. textbox1,textbox1,button1, textbox3,

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

    Re: [2005] Accept Enter Key

    Person 1: I have a piece of wood and I want to chop it in half. How do I do it?

    Person 2: Take your axe and swing it at the middle of the wood.

    Person 1: That worked for that piece of wood. I have three more pieces of wood. How would I chop them in half too?

    What do you think Person 1 should do? What works for one object will work for more than one object. You just have to do it more than once. If you've got code that works for one TextBox, don't you think it will work if you do the same thing for each of them?
    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

  7. #7
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: [2005] Accept Enter Key

    Quote Originally Posted by Hack
    Typically, I use the KeyPress even for enter rather than KeyDown.
    What is the advantage of the KeyPress event over the KeyDown event?

  8. #8
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: [2005] Accept Enter Key

    Quote Originally Posted by asm
    Code is working properly if only textbox is there , if i put button in between then it do not move to next textbox.
    ex. textbox1,textbox1,button1, textbox3,
    Set the TabStop property of the button to False or change its TabIndex property so that it does not receive focus at that point.

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