vba 重新关注 Excel 用户表单组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15442620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:04:27  来源:igfitidea点击:

Re-Focusing on an Excel Userform ComboBox

excelvbacontrolsfocususerform

提问by mikebinz

A User is Entering Data into the Edit Region of a ComboBox

用户正在将数据输入到 ComboBox 的编辑区域

The ComboBox's Change Event is used to run the following Code;

ComboBox 的 Change Event 用于运行以下代码;

    AppActivate "Microsoft Excel"
        :
    'Do some stuff
        :
    UserForm1.Show
    UserForm1.ComboBox1.SetFocus

This works OK, BUT; even though the ComboBox now has the Focus again (according to the Userform.ActiveControl anyway), it has no Insertion Pointer to indicate this fact, and the User has to Re-Select the ComboBox before he can continue entering Data

这工作正常,但是;即使组合框现在再次具有焦点(无论如何根据 Userform.ActiveControl),它没有插入指针来指示这一事实,并且用户必须重新选择组合框才能继续输入数据

I would like to have it so that the Insertion Pointer reappears and the User can continue Entering Data directly, without having to Re-Select the ComboBox

我想拥有它以便插入指针重新出现并且用户可以继续直接输入数据,而无需重新选择组合框

Adding the following line of Code

添加以下代码行

    SendKeys "{TAB}+{TAB}{RIGHT}"

is one solution, but it is messy (it generates ComboBox Events and using SendKeys is best avoided if possible anyway)

是一种解决方案,但它很乱(它会生成 ComboBox 事件,无论如何最好避免使用 SendKeys)

Can anyone suggest a better solution?

谁能提出更好的解决方案?

Q. Why do black sheep eat less than white sheep?

Q. 为什么黑羊比白羊吃的少?

A. Because there aren't as many of them

A. 因为没有那么多

回答by Patrick Lepelletier

the thing is , the userform itself might not have focus over the worksheet or other userforms.

问题是,用户表单本身可能没有关注工作表或其他用户表单。

Try this code:

试试这个代码:

with UserForm1.ComboBox1
    .Visible = False
    .Visible = True
    .setfocus
end with

or more generally to set properly focus, call this sub:

或者更一般地设置正确的焦点,调用这个子:

Sub Focus_ControlOfUserForm(ByRef Obj As Object) 'from the Userform, call Focus_ControlOfUserForm(Me)
Dim ctl As Control
With Obj
    Set ctl = .ActiveControl
    If TypeName(ctl) = "MultiPage" Or TypeName(ctl) = "Frame" Then
        Set ctl = ctl.SelectedItem.ActiveControl.Name
    End If

    With ctl
        Dim Af As Boolean
        With Application
            Af = .ScreenUpdating
            .ScreenUpdating = False
        End With
        '.SetFocus
        .Visible = False
        .Visible = True
        .SetFocus 
        If Af Then Application.ScreenUpdating = True
    End With
End With
End Sub

I also use that kind of code to move focus to the Form when i need ControlTipText showing on hover. If the Form has no focus, the Text bubbles won't show on mouse hover...

当我需要在悬停时显示 ControlTipText 时,我还使用这种代码将焦点移到表单上。如果表单没有焦点,则鼠标悬停时不会显示文本气泡...

回答by Ross McConeghy

After

UserForm1.ComboBox1.SetFocus

add

添加

UserForm1.ComboBox1.SelStart = 0 'set the selected text starting position to 0

回答by B Hart

At the end of the UserForm Change Event add the code:

在用户窗体更改事件的末尾添加代码:

Application.OnTime Now + TimeValue("00:00:01"), "GetComboBoxFocus", , True

Inside a Module add the code:

在模块内添加代码:

Sub GetComboBoxFocus()
    UserForm1.ComboBox1.SetFocus
End Sub

I've run into this several times. For some reason it will work when being called from a module. The One Second is hardly noticeable to the end user. If you prefer, you can also add it earlier in the event code, it will wait until the current routine is finished and then run immediately following (1 second - time taken for the remaining code). Hope this helps.

我已经遇到过好几次了。出于某种原因,它会在从模块中调用时起作用。最终用户几乎不会注意到一秒。如果您愿意,您也可以在事件代码中更早地添加它,它会等到当前例程完成,然后立即运行(1 秒 - 剩余代码所用的时间)。希望这可以帮助。