Link to home
Start Free TrialLog in
Avatar of rrowe68
rrowe68

asked on

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

Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

In vb6 or VB.NET ?
Nevermind is in .NET

You can do the same using the form keydown event and setting the keypreview = true
See KeyPreview on the Form
>>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
Avatar of rrowe68
rrowe68

ASKER

Which Form Level event does this code go in?

Thanks.
Yes it work in the textboxs. If you select a datagridview, button, etc you have to do manually.
Avatar of rrowe68

ASKER

Thanks...

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

 SendKeys.Send("{HOME}")
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

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

Avatar of rrowe68

ASKER

Idle_Mind -

Where does this code go and where is it envoked?
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.
Avatar of rrowe68

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of dentab
dentab
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rrowe68

ASKER

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

"Return bHandled "

Did that stop the beeping too?

nice one!
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....
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
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
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!
Avatar of rrowe68

ASKER

Sorry - can I split up the points.
Don't worry about it rrowe66...as dentab pointed out, I have points coming out my ears.
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.
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
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
thats VB6, this is.net