简体   繁体   English

Combobox.dropdown in Excel 显示在用户表单之外

[英]Combobox.dropdown in Excel shows outside the userform

I have created a userform and included a combobox populated with numbers 1 to x in userform.initialize and then added.setfocus and.dropdown commands.我创建了一个用户窗体,并在 userform.initialize 中包含一个 combobox,其中填充了数字 1 到 x,然后是 added.setfocus 和 .dropdown 命令。 For some strange reason the combobox (with dropdown) appears on my lefthand screen outside the userform.出于某种奇怪的原因,combobox(带下拉菜单)出现在用户窗体外的左侧屏幕上。 The combobox also appears on the userform, but without the dropdown displayed. combobox 也出现在用户窗体上,但没有显示下拉列表。 The screen combobox is active and if I click a number in the dropdown list the code accepts this input.屏幕 combobox 处于活动状态,如果我单击下拉列表中的数字,代码将接受此输入。

I have tried deleting the combobox and inserting another one with a different name, but the behaviour persists.我已尝试删除 combobox 并插入另一个具有不同名称的,但该行为仍然存在。 If I remove the.dropdown command, the combobox appears correctly on the userform without the screen copy and I can click the userform box and display and use the dropdown list.如果我删除 .dropdown 命令,则 combobox 会在没有屏幕副本的情况下正确显示在用户窗体上,我可以单击用户窗体框并显示和使用下拉列表。

On reading an unrelated post, I tried adding.visible=false, followed by.visible=true before the.dropdown command, but that didn't stop the behaviour.在阅读一篇不相关的文章时,我尝试在 .dropdown 命令之前添加.visible=false,然后添加.visible=true,但这并没有阻止这种行为。

I have tried exporting, deleting and re-importing the userform, but the behaviour persists.我已尝试导出、删除和重新导入用户窗体,但该行为仍然存在。

The code I am using (in userform_initialize sub) is:我正在使用的代码(在 userform_initialize 子中)是:

With cbxGroup 'combobox
   .Clear
   For i = 1 To PlayGroups
   .AddItem i
   Next
   .ListIndex = -1
   .Visible = False
   .Visible = True
   .SetFocus
   .DropDown
End With

Has anyone come across such behaviour before and can explain what has happened, and maybe how to fix it.以前有没有人遇到过这种行为,可以解释发生了什么,也许还有解决方法。 I can just recreate the userform, but it seems a lot of unnecessary work.我可以重新创建用户窗体,但似乎有很多不必要的工作。 I am using Office 365我正在使用 Office 365

I can confirm the behaviour.我可以确认这种行为。

I strongly assume that this is related to the fact that you are using the Initialize -Trigger.我强烈认为这与您使用Initialize -Trigger 这一事实有关。 When this trigger is executed, the form is still in the creation-process and some internal properties (eg the exact screen position) are likely not calculated.执行此触发器时,表单仍在创建过程中,并且可能未计算某些内部属性(例如确切的屏幕位置)。 As a consequence, when calling DropDown , the Combobox is painted on the screen but not at the right position.因此,在调用DropDown时,Combobox 会绘制在屏幕上,但不会绘制在右侧 position 上。

You can keep the code to fill the combobox in the Initialize -Trigger, but you should move the SetFocus and DropDown -commands to the Activate -Trigger您可以保留代码以在Initialize -Trigger 中填充 combobox,但您应该将SetFocusDropDown -commands 移动到Activate -Trigger

Private Sub UserForm_Initialize()
    fillCombo
End Sub

Private Sub UserForm_Activate()
    showCombo
End Sub

Sub fillCombo()
    With Me.cbxGroup 
       .Clear
       Dim i
       For i = 1 To PlayGroups
           .AddItem i
       Next
       .ListIndex = -1
    End With
End Sub

Sub showCombo()
    With Me.cbxGroup 
       .SetFocus
       .DropDown
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM