Public Function EnterToTab(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
SendKeys "{tab}"
KeyAscii = 0
End If
End Function
Private Sub txtUserCode_KeyPress(KeyAscii As Integer)
Call EnterToTab(KeyAscii)
End Sub
txtUserCode
包含存储在数据库中的特定用户的代码。txtUserCode
中输入任意数字并按输入时,它不会转到下一个文本框,它的keyascii变为49,不等于13。 答案 0 :(得分:2)
如何使用setFocus
方法切换到下一个文本字段而不是模拟 TAB ?
Private Sub txtUserCode_KeyPress(KeyAscii As Integer)
If (KeyAscii = vbKeyReturn) Then
txtNextTextField.setFocus
End If
End Sub
您还可以使用控件数组(表单中包含的所有文本字段的数组)并递增索引。因此,您可以将此代码用于表单的所有文本字段,而无需编写冗余代码。
因此,如果用户在文本字段索引0中按 return ,则将焦点设置为索引+ 1(= 1)。要创建控件数组,请复制第一个文本字段并将其粘贴到表单中。 VB6会询问您是否要创建一个控件数组。如果单击“是”,它将自动执行。然后您可以使用以下代码:
Private Sub txtField_KeyPress(Index As Integer, KeyAscii As Integer)
If (KeyAscii = vbKeyReturn) Then
If ((Index + 1) < txtField.Count) Then
txtField(Index+1).setFocus
Else
MsgBox "Reached end of form!"
End If
End If
End Sub
答案 1 :(得分:2)
KB142816 How To Make ENTER Key Move Focus Like TAB Key for VB Controls的参考实现与您的类似。但。它最重要的部分,IMO,是免责声明:
您可以使用ENTER键将焦点移动到控件上 下一个更高的TabIndex属性值,如TAB键那样。
但是,使用ENTER键移动焦点不会跟随 建议使用基于Microsoft Windows的应用程序设计指南。该 应使用ENTER键处理默认命令或进行处理 输入信息,而不是移动焦点。
无论如何,你的代码不起作用的原因是一个谜。由于 Tab 和 Enter 都不会从txtUserCode
字段移动焦点,我唯一的猜测是txtUserCode
是唯一具有TabStop
属性的字段设为True
。即没有其他控制可以将焦点转移到。
答案 2 :(得分:0)
MrSnurb提供的示例是一个良好的开端,但它有很多问题,例如,控件可能被禁用或不可见(setfocus将崩溃),你的控制器中的下一个控件没有&#39 ; t表示它也是下一个使用tab时会得到焦点的控件(你可以根据需要设置tabindex)。 我简单地想到了简单的&#39;例程(以及一个额外的函数),你可以用来转到窗体上的下一个或前一个控件(如果它与容器上的控件(框架或其他东西)一起工作,则实际上没有检查过),所以它可能需要额外的检查那个..
'##############################################################################
'##
'## Function fnControlCanHaveFocus
'##
'##############################################################################
'A separate routine, because On Error goto doesn't work with this type of
'error in the IDE within a For Each loop,
'even if you have set 'Only break on unhandled' errors
Private Function fnControlCanHaveFocus(ByRef ctrl As Control) As Boolean
On Error GoTo ErrorHandling
'--------------------------------------------------------------
'Check for properties which lets a control get a focus
'For now also Check TabStop even though the control CAN have focus if this is off
fnControlCanHaveFocus = (ctrl.TabStop And _
ctrl.Enabled And _
ctrl.Visible)
Exit Function
ErrorHandling:
fnControlCanHaveFocus = False
End Function
'##############################################################################
'##
'## Sub pSetFocusToNextControl
'##
'##############################################################################
Private Sub pSetFocusToNextControl(ByRef frm As Form)
Dim ctrl As Control
Dim ctrlFirst As Control
Dim ctrlNext As Control
'--------------------------------------------------------------
'Is there even an active control?
If Not frm.ActiveControl Is Nothing Then
'--------------------------------------------------------------
'Try and find the First and next control which can receive focus
Set ctrlFirst = Nothing
Set ctrlNext = Nothing
For Each ctrl In frm.Controls
'--------------------------------------------------------------
'Can this control have focus?
If fnControlCanHaveFocus(ctrl) And _
Not ctrl Is frm.ActiveControl Then
'--------------------------------------------------------------
'Check for Next control
If ctrl.TabIndex > frm.ActiveControl.TabIndex Then
If ctrlNext Is Nothing Then
Set ctrlNext = ctrl
ElseIf ctrlNext.TabIndex > ctrl.TabIndex Then
Set ctrlNext = ctrl
End If 'ElseIf ctrlNext.TabIndex>ctrl.TabIndex
End If 'If ctrl.TabIndex>frm.ActiveControl.TabIndex
'--------------------------------------------------------------
'Check for first control
If ctrlFirst Is Nothing Then
Set ctrlFirst = ctrl
ElseIf ctrlFirst.TabIndex < ctrl.TabIndex Then
Set ctrlFirst = ctrl
End If 'ElseIf ctrlFirst.TabIndex<ctrl.TabIndex
End If 'If fnControlCanHaveFocus(ctrl) And...
Next ctrl
'--------------------------------------------------------------
'Is there a next control to set focus to?
If Not ctrlNext Is Nothing Then
Call ctrlNext.SetFocus
'--------------------------------------------------------------
'No next control, but a first control to jump to?
ElseIf Not ctrlFirst Is Nothing Then
Call ctrlFirst.SetFocus
End If 'ElseIf Not ctrlFirst Is Nothing
End If 'If Not frm.ActiveControl Is Nothing
End Sub
'##############################################################################
'##
'## Sub pSetFocusToPreviousControl
'##
'##############################################################################
Private Sub pSetFocusToPreviousControl(ByRef frm As Form)
Dim ctrl As Control
Dim ctrlLast As Control
Dim ctrlPrevious As Control
'--------------------------------------------------------------
'Is there even an active control?
If Not frm.ActiveControl Is Nothing Then
'--------------------------------------------------------------
'Try and find the Last and previous control which can receive focus
Set ctrlLast = Nothing
Set ctrlPrevious = Nothing
For Each ctrl In frm.Controls
'--------------------------------------------------------------
'Can this control have focus?
If fnControlCanHaveFocus(ctrl) And _
Not ctrl Is frm.ActiveControl Then
'--------------------------------------------------------------
'Check for Previous control
If ctrl.TabIndex < frm.ActiveControl.TabIndex Then
If ctrlPrevious Is Nothing Then
Set ctrlPrevious = ctrl
ElseIf ctrlPrevious.TabIndex < ctrl.TabIndex Then
Set ctrlPrevious = ctrl
End If 'ElseIf ctrlPrevious.TabIndex<ctrl.TabIndex
End If 'If ctrl.TabIndex<frm.ActiveControl.TabIndex
'--------------------------------------------------------------
'Check for Last control
If ctrlLast Is Nothing Then
Set ctrlLast = ctrl
ElseIf ctrlLast.TabIndex > ctrl.TabIndex Then
Set ctrlLast = ctrl
End If 'ElseIf ctrlLast.TabIndex>ctrl.TabIndex
End If 'If fnControlCanHaveFocus(ctrl) And...
Next ctrl
'--------------------------------------------------------------
'Is there a previous control to set focus to?
If Not ctrlPrevious Is Nothing Then
Call ctrlPrevious.SetFocus
'--------------------------------------------------------------
'No previous control but a Last control to jump to?
ElseIf Not ctrlLast Is Nothing Then
Call ctrlLast.SetFocus
End If 'ElseIf Not ctrlLast Is Nothing
End If 'If Not frm.ActiveControl Is Nothing
End Sub
你可以像这样使用它:
Private Sub txt_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyDown, _
vbKeyReturn
Call pSetFocusToNextControl(Me)
KeyCode = 0
Case vbKeyUp
Call pSetFocusToPreviousControl(Me)
KeyCode = 0
End Select
End Sub