控制列表框的样式和选择行为

以下示例使用 ListStyleMultiSelect 属性来控制 ListBox 的外观。 用户使用 ToggleButtonListStyle 选择一个值,并为其中一个 MultiSelect 值选择 OptionButtonListBox 的外观以及 ListBox 中的选择行为相应地发生更改。

若要使用本示例,请将此示例代码复制到窗体的"脚本编辑器"中。 若要运行本代码,需要打开该窗体,以便激活 Open 事件。 确保该窗体包含:

  • 一个名为"ListBox1"的 ListBox

  • 一个名为"Label1"的 Label

  • 三个名称分别为"OptionButton1"到"OptionButton3"的 OptionButton 控件。

  • 一个名为"ToggleButton1"的 ToggleButton

Sub Item_Open() 
 Set ListBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ListBox1") 
 Set Label1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Label1") 
 Set OptionButton1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("OptionButton1") 
 Set OptionButton2 = Item.GetInspector.ModifiedFormPages("P.2").Controls("OptionButton2") 
 Set OptionButton3 = Item.GetInspector.ModifiedFormPages("P.2").Controls("OptionButton3") 
 Set ToggleButton1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ToggleButton1") 
 
 For i = 1 To 8 
 ListBox1.AddItem "Choice" & (ListBox1.ListCount + 1) 
 Next 
 
 Label1.Caption = "MultiSelect Choices" 
 Label1.AutoSize = True 
 
 ListBox1.MultiSelect = 0 '0=fmMultiSelectSingle 
 OptionButton1.Caption = "Single entry" 
 OptionButton1.Value = True 
 OptionButton2.Caption = "Multiple entries" 
 OptionButton3.Caption = "Extended entries" 
 
 ToggleButton1.Caption = "ListStyle - Plain" 
 ToggleButton1.Value = True 
 ToggleButton1.Width = 90 
 ToggleButton1.Height = 30 
End Sub 
 
Sub OptionButton1_Click() 
 Set ListBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ListBox1") 
 ListBox1.MultiSelect = 0 '0=fmMultiSelectSingle 
End Sub 
 
Sub OptionButton2_Click() 
 Set ListBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ListBox1") 
 ListBox1.MultiSelect = 1 '1=fmMultiSelectMulti 
End Sub 
 
Sub OptionButton3_Click() 
 Set ListBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ListBox1") 
 ListBox1.MultiSelect = 2 '2=fmMultiSelectExtended 
End Sub 
 
Sub ToggleButton1_Click() 
 Set ToggleButton1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ToggleButton1") 
 Set ListBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ListBox1") 
 
 If ToggleButton1.Value = True Then 
 ToggleButton1.Caption = "Plain ListStyle" 
 ListBox1.ListStyle = 0 '0=fmListStylePlain 
 Else 
 ToggleButton1.Caption = "OptionButton or CheckBox" 
 ListBox1.ListStyle = 1 '1=fmListStyleOption 
 End If 
End Sub

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。