按Enter键后,请继续关注文本框 [英] Keep focus on textbox after pressing enter

查看:82
本文介绍了按Enter键后,请继续关注文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VBA表单中按Enter键后,如何将焦点保持在文本框中?

How can I keep the focus in a textbox after pressing enter in a VBA form?

此代码将文本添加到列表框,我想将重点放在文本框上,以准备接收其他项目.

This code adds the text to a Listbox and I want to keep the focus on the textbox to get ready to receive another item.

当我单击添加按钮时,它将文本添加到列表框,并将焦点返回到文本框,但是当我按Enter键时,它并没有,即使使用相同的代码也是如此.有什么建议吗?

When I click in the button Add, it adds the text to the Listbox and returns the focus to the textbox, howerver when I press enter it doesn't, even tough it uses the same code. Any suggestion?

这是我的文本框代码:

Private Sub TxtOtherAsset_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    If KeyCode = 13 Then
        CmdAddOtherAsset_Click
    End If

End Sub

这是我的按钮的代码:

Private Sub CmdAddOtherAsset_Click()

    If TxtOtherAsset.Text <> "" Then
        ListAddedAssets.AddItem TxtOtherAsset.Text
        TxtOtherAsset.Text = ""
    End If

    TxtOtherAsset.SetFocus

End Sub

我尝试了几种方法,但是无法将焦点返回到文本框.按Enter键后,焦点将移至TabIndex中的下一个.

I've tried several ways, but I'm not able to return the focus to the textbox. After pressing enter, the focus goes to the next in the TabIndex.

推荐答案

从我的头开始:尝试将KeyCode设置为0.另外,使用KeyCodeConstants类(来自Core库)确定Enter键的值是.

From the top of my head: try setting the KeyCode to 0. Also, use the KeyCodeConstants class (from the Core library) to determine what value the Enter key is.

赞:

If KeyCode = KeyCodeConstants.vbKeyReturn Then
    CmdAddOtherAsset_Click
    KeyCode = 0
End If

删除试图将焦点设置在另一个子(TxtOtherAsset.SetFocus)上的行.

Remove the line you're trying to set the focus on the other sub (TxtOtherAsset.SetFocus).

希望它对您有用.我没有测试.

Hope it works for you. I did not test it.

这篇关于按Enter键后,请继续关注文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆