vba 使 PowerPoint 组合框正确列出项目的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2834713/
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 11:39:14  来源:igfitidea点击:

Problems getting PowerPoint Combobox to list items correctly

vbacomboboxpowerpoint

提问by mike toyn

I am creating a PowerPoint in which I want users to be able to select an item from a list in a combo box. Nothing needs to happen after this, it is just to provide a record, on screen, of their choice.

我正在创建一个 PowerPoint,我希望用户能够在其中从组合框中的列表中选择一个项目。在此之后不需要发生任何事情,只需在屏幕上提供他们选择的记录即可。

My problem is that I seem to either be able to populate the combo box and users can select an item but the list gets longer each time the combobox is clicked on (i.e each time it is clicked on the list gets duplicated). Or alternatively, I can clear the combo box, then populate it but in this scenario, the users choice also seems to get cleared.

我的问题是我似乎能够填充组合框并且用户可以选择一个项目,但是每次单击组合框时列表都会变长(即每次单击列表时都会重复)。或者,我可以清除组合框,然后填充它,但在这种情况下,用户的选择似乎也被清除了。

VBA example 1:

VBA 示例 1:

Private Sub ComboBox1_DropButtonClick()
With ComboBox1
.AddItem " ", 0
.AddItem "speed", 1
.AddItem "provisionality", 2
.AddItem "automation", 3
.AddItem "replication", 4
.AddItem "communicability", 5
.AddItem "multi-modality", 6
.AddItem "non-linearity", 7
.AddItem "capacity", 8
.AddItem "interactivity", 9
End With
End Sub

VBA example 2:

VBA 示例 2:

Private Sub ComboBox1_DropButtonClick()
ComboBox1.Clear
With ComboBox1
.AddItem " ", 0
.AddItem "speed", 1
.AddItem "provisionality", 2
.AddItem "automation", 3
.AddItem "replication", 4
.AddItem "communicability", 5
.AddItem "multi-modality", 6
.AddItem "non-linearity", 7
.AddItem "capacity", 8
.AddItem "interactivity", 9
End With
End Sub

Can anyone help?

任何人都可以帮忙吗?

回答by dendarii

Because you have your code in the DropButtonClick event, every time you click the dropdown, those items are being added to your combobox. Try adding some code to check if the combobox is already populated before it adds the items:

因为您在 DropButtonClick 事件中有您的代码,所以每次您单击下拉列表时,这些项目都会被添加到您的组合框中。尝试添加一些代码以在添加项目之前检查组合框是否已填充:

Private Sub ComboBox1_DropButtonClick()

    If ComboBox1.ListCount = 0 Then
        With ComboBox1
            .AddItem " ", 0
            .AddItem "speed", 1
            .AddItem "provisionality", 2
            .AddItem "automation", 3
            .AddItem "replication", 4
            .AddItem "communicability", 5
            .AddItem "multi-modality", 6
            .AddItem "non-linearity", 7
            .AddItem "capacity", 8
            .AddItem "interactivity", 9
        End With
    End If

End Sub

Then you don't have to clear the combobox and clear the selected item along with it.

然后您不必清除组合框并清除所选项目。