Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to detect 'Enter' keypress and process tab key

Status
Not open for further replies.

flbos

Programmer
Sep 15, 2005
41
NL
Hello,

For a certain form I want the enter key to perform exactly the same action as the tab key. I thought I would be able to do something like this:

Code:
    Private Sub frmAfsluiten_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Enter Then
            System.Windows.Forms.SendKeys.Send(Keys.Tab)
        End If
    End Sub

However this doesn't work because the event is not fires when I press the enter key when the cursor is in a text box or other control. I want the cursor to move to the control with the next tab index when pressing enter but I don't know which event I need to use here.
 
First, your code is in the form's KeyDown event handler, so this will only execute when the form has focus. To make it work for your textboxes or other controls, you will have to put the code in the KeyDown event handler for each of those controls.

Another option is making a "generic" KeyDown event handler that does what you want, then assigning the required controls' KeyDown events to this handler, as one handler can handle events from multiple objects.

Second, once you get the code into the correct handler(s), it still will not work. To make it work the way you want, change this:

System.Windows.Forms.SendKeys.Send(Keys.Tab)

to this:

System.Windows.Forms.SendKeys.Send("{Tab}")

Hope this helps.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
OR

Private Sub frmTroublCalls_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar.ToString = Microsoft.VisualBasic.ChrW(13) Then
e.Handled = True
SendKeys.Send("{TAB}")
End If
End Sub
 
flbos, the only reason the Event doesn't fire is because you probably haven't set the Form's KeyPreview property to True (it defaults to False).

Once you've changed that your code will work (using SendKeys.Send("{TAB}")).

One problem though, the Enter Key will still be procesed by the control. The following helps with regard to a TextBox, butt you may need to experiment to see the effect on other controls.

Code:
  [Blue]Private[/Blue] [Blue]Sub[/Blue] Form1_KeyDown([Blue]ByVal[/Blue] sender [Blue]As[/Blue] Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.Windows.Forms.KeyEventArgs) [Blue]Handles[/Blue] [Blue]MyBase[/Blue].KeyDown

    [Blue]If[/Blue] e.KeyCode = Keys.Enter [Blue]Then[/Blue]
      e.Handled = [Blue]True[/Blue]
      [Green]'possibly also add the following three lines[/Green]
      [Blue]If[/Blue] TypeOf ActiveControl [Blue]Is[/Blue] TextBox [Blue]Then[/Blue]
        SendKeys.Send([Red]"{BACKSPACE}"[/Red])
      [Blue]End[/Blue] [Blue]If[/Blue]
      SendKeys.Send([Red]"{TAB}"[/Red])
    [Blue]End[/Blue] [Blue]If[/Blue]

  [Blue]End[/Blue] [Blue]Sub[/Blue]

Hope this helps.

[vampire][bat]
 
Thanks for the answers, it works fine now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top