本文介绍了VBA用户窗体:在文本框上使用SetFocus后出现意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UserForm,它显示活动工作表中各个单元格的文本内容,并有一个TextBox,允许用户输入新信息.

I have a UserForm that displays the text content of various cells within the active worksheet and has a TextBox to allow users to enter new information.

每当用户通过Workbook_SheetSelectionChange事件选择新的单元格/范围时,UF就会自动更新.

The UF updates automatically whenever the user selects a new cell/range through the Workbook_SheetSelectionChange event.

在UF更新过程结束时(UF代码模块内的公共子程序),我使用TextBoxName.SetFocus(以及.SelStart = .TextLenght)将焦点设置在文本框上,以供用户启动打字.

At the end of the UF Update procedure (a public sub within the UF code module) I use TextBoxName.SetFocus (along with .SelStart = .TextLenght) to set the focus on the text box, ready for the user to start typing.

现在,当UF通过UserForm_Activate事件激活时,此更新过程也将运行.

Now this update procedure also runs when the UF activates through the UserForm_Activate event.

这是我的问题

当UF激活时,焦点将成功设置在TextBox上,光标将显示在文本的末尾,并且我键入的内容将按预期输入到TextBox中.

When the UF activates, the focus is successfully set on the TextBox , with the cursor visible at the end of the text, and whatever I type is entered in the TextBox as expected.

但是,如果我单击一个新单元格,该单元格运行相同的更新过程,但Workbook_SheetSelectionChange事件除外,则发生一些奇怪的事情.从技术上讲,焦点在TextBox上,但是光标不可见.空格键,Enter和Backspace均按预期工作,但是字母和数字键无效.IE.我可以删除文本或点击Enter以添加新行,但是如果我点击其他任何数字/字母键,都不会发生任何事情,直到再次使用鼠标单击TextBox为止.

BUT, if I click on a new cell, which runs the same update procedure except from the Workbook_SheetSelectionChange event, something weird happens. The focus is technically on the TextBox, but the cursor isn't visible. Spacebar, Enter and Backspace all work as expected, however the letters and number keys don't. I.e. I can delete text or tap enter to add new lines, but if I tap any other number/letter key nothing happens, until I use the mouse to click on the TextBox again.

我尝试在不同位置使用.SetFocus将程序移出UF模块,包括在Workbook_SheetSelectionChange调用更新过程之后,但无济于事.

I've tried moving the procedure out of the UF module, using .SetFocus at different places, including after the Workbook_SheetSelectionChange calls the update procedure, but nothing works.

我还能尝试什么?

提前谢谢!

推荐答案

再次使文本框保持合作状态

某些(窗口)进程可能会阻碍 .SetFocus 发挥作用,例如,在文本框验证期间显示的 MsgBox 窗口,甚至是根据您的情况,单击工作表单元格时,由 Workbook_SelectionChange 事件引发的完整更新过程.尽管您明确地设置了焦点,或者更确切地说是因为,但控件内部并没有失去(完全)焦点,因此控件的焦点将不会/无法重置,因为用户窗体已经拥有".

Some (window) processes can hinder the .SetFocus to show full effect, such as a MsgBox window displayed during textbox validation or even acomplete update procedure raised by the Workbook_SelectionChange event while clicking on a worksheet cell as in your case.Though you explicitly set focus or rather because the control internally didn't lose (complete) focus,so the control's focus won't/can't be reset as the Userform "has" it already.

要克服不满意的情况,您可以

To overcome the unsatisfactory situation you can

  1. 切换并重新切换 .Enabled 属性,或者选择
  2. 通过任何其他接受焦点的控件(包括命令按钮或框架)明确失去焦点,然后再次重置焦点.

相关链接

要获取更多见解,我建议查看

To get more insight I recommend to see the explanations given at Validation message of textbox entry on modeless userform interrupts text selection

致谢常识

这篇关于VBA用户窗体:在文本框上使用SetFocus后出现意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 11:40