ListStyle、MultiSelect 属性示例ListStyle, MultiSelect properties example

下面的示例使用**ListStyle** 和多**** 属性来控制**ListBox** 的外观。The following example uses the ListStyle and MultiSelect properties to control the appearance of a ListBox.

用户使用**切换按钮** 选择ListStyle的值, 并为其中一个多值选择一个**选项按钮**。The user chooses a value for ListStyle using the ToggleButton and chooses an OptionButton for one of the MultiSelect values. ListBox 的外观以及 ListBox 中的选择行为相应地发生更改。The appearance of the ListBox changes accordingly, as well as the selection behavior within the ListBox.

若要使用此示例,请将此示例代码复制到窗体的声明部分。To use this example, copy this sample code to the Declarations portion of a form. 确保该窗体包含:Make sure that the form contains:

  • 一个名为"ListBox1"的 ListBoxA ListBox named ListBox1.
  • 一个名为"Label1"的 LabelA Label named Label1.
  • 三个名称分别为"OptionButton1"到"OptionButton3"的 OptionButton 控件。Three OptionButton controls named OptionButton1 through OptionButton3.
  • 一个名为"ToggleButton1"的 ToggleButtonA ToggleButton named ToggleButton1.
Private Sub UserForm_Initialize() 
 Dim i As Integer 
 For i = 1 To 8 
 ListBox1.AddItem "Choice" & (ListBox1.ListCount + 1) 
 Next i 
 Label1.Caption = "MultiSelect Choices" 
 Label1.AutoSize = True 
 ListBox1.MultiSelect = 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 
Private Sub OptionButton1_Click() 
 ListBox1.MultiSelect = fmMultiSelectSingle 
End Sub 
Private Sub OptionButton2_Click() 
 ListBox1.MultiSelect = fmMultiSelectMulti 
End Sub 
Private Sub OptionButton3_Click() 
 ListBox1.MultiSelect = fmMultiSelectExtended 
End Sub 
Private Sub ToggleButton1_Click() 
 If ToggleButton1.Value = True Then 
 ToggleButton1.Caption = "Plain ListStyle" 
 ListBox1.ListStyle = fmListStylePlain 
 ToggleButton1.Caption = "OptionButton " _ 
 & "or CheckBox" 
 ListBox1.ListStyle = fmListStyleOption 
 End If 
End Sub

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.