Hello everyone,
i want use EnterKey in my Project ,i write some code in from load,but still TAB key use in my project. Here is a code,

If KeyAscii = 13 Then
SendKeys vbEnter
End If

Dani AI

Generated

Several replies point in useful directions but miss scale and edge cases. tried resending Enter; and hinted at form-level handling; showed per-control handlers; and noted command buttons' default behavior. For a form with many inputs, a single form-level routine that advances focus along the tab order is more maintainable than wiring every control.

Strategy: on Enter, locate the active control's TabIndex and move focus to the next visible, enabled, tabbable control. Skip multiline fields and any control that must keep Enter for its own behavior. The example below uses CallByName to test properties safely (so controls that lack a property won't raise errors) and wraps the search if needed.

' Put these helpers in the form's code module. The form should have KeyPreview = True.

Private Function HasProperty(obj As Object, propName As String) As Boolean
  On Error Resume Next
  Dim tmp
  tmp = CallByName(obj, propName, VbGet)
  HasProperty = (Err.Number = 0)
  Err.Clear
End Function

Private Function IsTabbable(ctrl As Control) As Boolean
  On Error Resume Next
  If Not ctrl.Visible Then Exit Function
  If CallByName(ctrl, "Enabled", VbGet) = False Then Exit Function
  If HasProperty(ctrl, "TabStop") Then
    If CallByName(ctrl, "TabStop", VbGet) = False Then Exit Function
  End If
  IsTabbable = True
End Function

Private Sub NextTabControl(frm As Object)
  Dim c As Control, candidate As Control
  Dim curr As Long, bestIndex As Long
  On Error Resume Next
  curr = frm.ActiveControl.TabIndex
  bestIndex = 99999
  For Each c In frm.Controls
    If c.TabIndex > curr Then
      If IsTabbable(c) Then
        If c.TabIndex < bestIndex Then
          Set candidate = c
          bestIndex = c.TabIndex
        End If
      End If
    End If
  Next
  If Not candidate Is Nothing Then candidate.SetFocus: Exit Sub
  bestIndex = 99999
  For Each c In frm.Controls
    If IsTabbable(c) Then
      If c.TabIndex < bestIndex Then
        Set candidate = c
        bestIndex = c.TabIndex
      End If
    End If
  Next
  If Not candidate Is Nothing Then candidate.SetFocus
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
  If KeyAscii <> 13 Then Exit Sub
  Dim ctl As Control
  On Error Resume Next
  Set ctl = Me.ActiveControl
  If Not ctl Is Nothing Then
    If HasProperty(ctl, "MultiLine") Then
      If CallByName(ctl, "MultiLine", VbGet) = True Then Exit Sub
    End If
  End If
  NextTabControl Me
  KeyAscii = 0  ' swallow Enter so it doesn't trigger a default button unintentionally
End Sub

Notes and troubleshooting: confirm TabIndex and TabStop for all inputs; multiline textboxes must be skipped or left TabStop = True depending on UX; if a form needs an Enter-to-submit button (login dialog), prefer the button's Default property rather than globally converting Enter to Tab; test with combo boxes and ActiveX controls since some handle focus differently.

Recommended Answers

All 8 Replies

Hello everyone,
i want use EnterKey in my Project ,i write some code in from load,but still TAB key use in my project. Here is a code,

If KeyAscii = 13 Then
SendKeys vbEnter
End If

Well the KeyAscii for the Enter key is 13 as you've stated. So why if the Enter key is pressed you are pressing it again with SendKeys?
If the Enter key is pressed then you surely want to do some other task?

It will depend on which control you are looking for the Enter key. You may have to use Me.KeyPreview = True in Form_Load to catch keystrokes.

You say the Tab key is working, don't you want the Tab key working? You can turn the Tab key off on any control look for the TabStop property. :icon_wink:

Hey...
The code that you have written is correct..
keyascii=13 is used for the "Enter" key response..
Instead of writin this code in the Form_load() event, write it under General Declarations..

By doing so , u r makin this code global to your form...
Let me knw... :cool:

Do you mean something like this???

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then Text2.SetFocus
End Sub

Private Sub Text2_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then Text1.SetFocus
End Sub

or like this???

Private Sub Text1_KeyUp(Index As Integer, KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
  Index = Index + 1
  If Index > (Text1.Count - 1) Then
    'code to set focus to next control
    Text2.SetFocus
  Else
    Text1(Index).SetFocus
  End If
End If
End Sub

Good Luck

Thank u 4 ur help, but my problem is that i have many control in my form level ,how it possible to control my form by using single line statement

In vb6 you can't. You can however use the tab order to specify the order of the controls as they are tabbed to. Also command buttons when you hit enter activate the click event. Then you can reduce your code for textboxes if you use arrays.


Good Luck

Hey...
The code that you have written is correct..
keyascii=13 is used for the "Enter" key response..
Instead of writin this code in the Form_load() event, write it under General Declarations..

By doing so , u r makin this code global to your form...
Let me knw... :cool:

But I don't see any reference to the Form_load() event in that code. Where do you get that from? :icon_wink:

But I don't see any reference to the Form_load() event in that code. Where do you get that from? :icon_wink:

M bad..woops...!!

If you want to use the enter key in you project, for example there is a Log-In form, where there are "username" and "password" textboxes and an "Ok" and "Cancel" button, Then when the user presses the enter key, the Ok button must be activated or the click event of the control must be triggered.

you can set it in the property box of the control:

Default = True

but, by default it is equals to False, so you have to change it manually or make a code for it in the code window, like this:

Private Sub Form_Load()
cmdOk.Default = True
End Sub


Thanks and Good Luck!...^_^...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.