当按下Enter键时,如何防止光标移动到下一个文本框



当文本框为空并且按下Enter键时,我想防止光标移动到下一个文本框。假设我有两个文本框,Textbox1和Textbox2,我想要的是,除非我在Textbox1中输入任何内容,否则当按下回车键时,它不应该允许将光标从Textbox1移动到Textbox2。但它应该允许使用鼠标或箭头键手动移动光标。希望它能解释一切。我试着跟随

Private Sub txtCode_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If Trim(Me.txtCode) = "" And KeyCode = 13 Then
Me.txtCode.SetFocus
End If
End Sub

但没有好的

你很接近。尝试以下操作:

Private Sub txtCode_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If Trim(Me.txtCode) = "" And KeyCode = 13 Then
KeyCode = 0
End If
End Sub

将KeyCode设置为0可以有效地取消击键。

最新更新