ListBox 控件、BoundColumn 属性示例ListBox control, BoundColumn property example

下面的示例演示**BoundColumn** 属性如何影响**ListBox** 的值。The following example demonstrates how the BoundColumn property influences the value of a ListBox. 用户可以选择将 ListBox 的值设置为 ListBox 中指定数据行或指定数据列的索引值。The user can choose to set the value of the ListBox to the index value of the specified row, or to a specified column of data in the ListBox.

若要使用此示例,请将此示例代码复制到窗体的 Declarations 部分。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"、"OptionButton2"和"OptionButton3"的 OptionButton 控件。Three OptionButton controls named OptionButton1, OptionButton2, and OptionButton3.
Private Sub UserForm_Initialize() 
 ListBox1.ColumnCount = 2 
 ListBox1.AddItem "Item 1, Column 1" 
 ListBox1.List(0, 1) = "Item 1, Column 2" 
 ListBox1.AddItem "Item 2, Column 1" 
 ListBox1.List(1, 1) = "Item 2, Column 2" 
 ListBox1.Value = "Item 1, Column 1" 
 OptionButton1.Caption = "List Index" 
 OptionButton2.Caption = "Column 1" 
 OptionButton3.Caption = "Column 2" 
 OptionButton2.Value = True 
End Sub 
Private Sub OptionButton1_Click() 
 ListBox1.BoundColumn = 0 
 Label1.Caption = ListBox1.Value 
End Sub 
Private Sub OptionButton2_Click() 
 ListBox1.BoundColumn = 1 
 Label1.Caption = ListBox1.Value 
End Sub 
Private Sub OptionButton3_Click() 
 ListBox1.BoundColumn = 2 
 Label1.Caption = ListBox1.Value 
End Sub 
Private Sub ListBox1_Click() 
 Label1.Caption = ListBox1.Value 
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.