We help IT Professionals succeed at work.

Keypress Event Handler - Send Tab command when Enter is pressed

rrowe68
rrowe68 asked
on
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

Comment
Watch Question

Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
In vb6 or VB.NET ?
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
Nevermind is in .NET

You can do the same using the form keydown event and setting the keypreview = true

Commented:
See KeyPreview on the Form
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
>>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

Author

Commented:
Which Form Level event does this code go in?

Thanks.
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
Yes it work in the textboxs. If you select a datagridview, button, etc you have to do manually.

Author

Commented:
Thanks...

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

 SendKeys.Send("{HOME}")

Commented:
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

Commented:
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.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

Commented:
Idle_Mind -

Where does this code go and where is it envoked?

Commented:
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.

Author

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
Commented:
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.

Author

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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Commented:
"Return bHandled "

Did that stop the beeping too?

nice one!
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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....
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
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

Commented:
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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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!

Author

Commented:
Sorry - can I split up the points.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Don't worry about it rrowe66...as dentab pointed out, I have points coming out my ears.

Commented:
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.

Commented:
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

Commented:
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

Commented:
thats VB6, this is.net