Keypress Event Handler - Send Tab command when Enter is pressed

I have 100 text box controls on a Form.    Every time I hit the Enter Key, I want the cursor to jump to the next control.... I have set the Tab Order Correctly and am good there.

This code works fine if I copy and paste it into every KeyPress Event in every TextBox.

        If Asc(e.KeyChar) = Keys.Enter Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        End If

Is there an easier way?   Can this be done at the Form Level?

Along the same lines - is there a snipet to have the cursor jump to the left side of the text box...  me.textbox.SelectionLength = 0

rrowe68Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jorge PaulinoIT Pro/DeveloperCommented:
In vb6 or VB.NET ?
0
Jorge PaulinoIT Pro/DeveloperCommented:
Nevermind is in .NET

You can do the same using the form keydown event and setting the keypreview = true
0
peetmCommented:
See KeyPreview on the Form
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Jorge PaulinoIT Pro/DeveloperCommented:
>>Along the same lines - is there a snipet to have the cursor jump to the left side of the text box...  me.textbox.SelectionLength = 0

You can use Me.TextBox1.Select(0,0) ' This will not work if you click using the mouse.

If you want to change your code above you can add another sendkey to do that

      If Asc(e.KeyChar) = Keys.Enter Then
            SendKeys.Send("{TAB}")
            SendKeys.Send("{HOME}")
            e.Handled = True
        End If
0
rrowe68Author Commented:
Which Form Level event does this code go in?

Thanks.
0
Jorge PaulinoIT Pro/DeveloperCommented:
Yes it work in the textboxs. If you select a datagridview, button, etc you have to do manually.
0
rrowe68Author Commented:
Thanks...

Adding the line actually causes it to skip one control (i.e. text box)

 SendKeys.Send("{HOME}")
0
dentabCommented:
Are the textboxes in a control array?

If so then

Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
  If Index < Text1.UBound Then
    'Do whatever you need to do on last textbox
  Else
    Text1(Index).SetFocus
  End If
End Sub

Open in new window

0
dentabCommented:
Sorry about my last comment, its a VB6 solution.

I do not have .Net installed on this PC, but I would loop through the controls collection on the form, testing the tabindex for being 1 greater than the current one and setting focus that way.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
A different approach...
Public Class Form1
 
    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Enter Then
            Dim ctl As Control = Me.ActiveControl
            If Not (ctl Is Nothing) Then
                If TypeOf ctl Is TextBox Then
                    SendKeys.Send("{TAB}")
                    Exit Function
                End If
            End If
        End If
 
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
 
End Class

Open in new window

0
rrowe68Author Commented:
Idle_Mind -

Where does this code go and where is it envoked?
0
dentabCommented:
Inside the form code I believe.  I am assuming that ProcessCmdKey is a standard method of the form, this code would then "just work".

Let me know if it does, I would be very interested.
0
rrowe68Author Commented:
Dentab - it seems to work.    Additional Question.

1)  How do you get rid of the 'beep'
2)  add checkbox control and dropdown (I have those too).

Thanks
0
dentabCommented:
Hmm, I'm not sure how to get rid of the beep...
1)
in VB6 I have done simular, and I had to essentially clear the equivilant of keyData.  In this example it is being passed byval so I don't know if it will work, although being vb 2005 and passing object references byval etc, you never know.

2) I dont have VB 2005 on this machine (or even .net) but it would be something like

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Enter Then
            Dim ctl As Control = Me.ActiveControl
            If Not (ctl Is Nothing) Then
                If (TypeOf ctl Is TextBox) or (TypeOf ctl Is CheckBox) or (TypeOf ctl Is combobox) Then
                    SendKeys.Send("{TAB}")
                    Exit Function
                End If
            End If
        End If
 
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

If I have guessed the names CheckBox and combobox wrong, then you'll need to change them.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rrowe68Author Commented:
This worked - Thanks.

  Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Dim bHandled As Boolean

        If keyData = Keys.Enter Then
            Dim ctl As Control = Me.ActiveControl
            If Not (ctl Is Nothing) Then
                If TypeOf ctl Is ComboBox Then
                    SendKeys.Send("{TAB}")
                    bHandled = True
                End If
                If TypeOf ctl Is TextBox Then
                    SendKeys.Send("{TAB}")
                    bHandled = True
                End If
                If TypeOf ctl Is CheckBox Then
                    SendKeys.Send("{TAB}")
                    bHandled = True
                End If
            End If
        End If

        Return bHandled   'MyBase.ProcessCmdKey(msg, keyData)
    End Function
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
To get rid of the beep, return true from the function like so:

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Enter Then
            Dim ctl As Control = Me.ActiveControl
            If Not (ctl Is Nothing) Then
                If (TypeOf ctl Is TextBox) OrElse (TypeOf ctl Is CheckBox) OrElse (TypeOf ctl Is ComboBox) Then
                    SendKeys.Send("{TAB}")
                    Return True
                End If
            End If
        End If

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

If you want to simply make Enter become Tab irregardless of the type of control then use:
Public Class Form1
 
    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Enter Then
            SendKeys.Send("{TAB}")
            Return True
        End If
 
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
 
End Class

Open in new window

0
dentabCommented:
"Return bHandled "

Did that stop the beeping too?

nice one!
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol....so dentab, who says:

    "I dont have VB 2005 on this machine (or even .net) "

Gets credit for MY solution.   =\

See my last post for how to get rid of the beep....
0
Jorge PaulinoIT Pro/DeveloperCommented:
rrowe68,

You have accepted the dentab solution but the original code belongs to Idle_Mind. You should at least give i'm some points. That's what I think (maybe i'm wrong)

jpaulino
0
dentabCommented:
Sorry Idle, I was only trying to help - I was hoping for an assist for the
2)  add checkbox control and dropdown

although looking at your profile, atleast you dont seem to need the points - wow
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's no big deal dentab...and I don't view YOU as having any negative participation at all.  =)

I just found it humorous that you don't have .Net on your machine and you got credit for the .Net solution!
0
rrowe68Author Commented:
Sorry - can I split up the points.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Don't worry about it rrowe66...as dentab pointed out, I have points coming out my ears.
0
dentabCommented:
lol

I do have it at work though, its just I cant test it on this machine.  All credit to you I wouldnt have thought of overriding... I think VB6 has broken me.
0
dentabCommented:
Oh well, if its not about the points then for any readers

Idle Mind is a genius (really its his rating), and the elegant solution was all his idea.
Thanks rrowe68 for the points though, it was enough to qualify me for another month of free EE ;)

Best of luck to both of you
0
23637269Commented:
If you just want the enter to act like a tab within the textbox and NO BEEP:

In the KeyPress Event of the textbox:

    If KeyAscii = 13 Then
        SendKeys vbTab
        KeyAscii = 0 'Prevent Beep
    End If
0
dentabCommented:
thats VB6, this is.net
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.