列表框控件、BoundColumn 属性示例

下例演示 BoundColumn 属性如何影响列表框的值。用户可选择是将列表框的值设置为指定行的索引值,还是设置为列表框中数据的指定列。

窗体包含:

  • 名为 ListBox1 的列表框。
  • 名为 Label1 的标签。
  • 名为 OptionButton1、OptionButton2 和 OptionButton3 的三个选项按钮控件。
示例
金字塔
Sub UserForm14_Initialize()
    UserForm14_ListBox1.ColumnCount = 2

    UserForm14_ListBox1.AddItem "Item 1, Column 1"
    UserForm14_ListBox1.List(0, 1) = "Item 1, Column 2"
    UserForm14_ListBox1.AddItem "Item 2, Column 1"
    UserForm14_ListBox1.List(1, 1) = "Item 2, Column 2"
    
    UserForm14_ListBox1.Value = "Item 1, Column 1"
    
    UserForm14_OptionButton1.Caption = "List Index"
    UserForm14_OptionButton2.Caption = "Column 1"
    UserForm14_OptionButton3.Caption = "Column 2"
    UserForm14_OptionButton2.Value = True
End Sub

Sub UserForm14_OptionButton1_Click()
    UserForm14_ListBox1.BoundColumn = 0
    UserForm14_Label1.Caption = UserForm14_ListBox1.Value
End Sub

Sub UserForm14_OptionButton2_Click()
    UserForm14_ListBox1.BoundColumn = 1
    UserForm14_Label1.Caption = UserForm14_ListBox1.Value
End Sub

Sub UserForm14_OptionButton3_Click()
    UserForm14_ListBox1.BoundColumn = 2
    UserForm14_Label1.Caption = UserForm14_ListBox1.Value
End Sub

Sub UserForm14_ListBox1_Click()
    UserForm14_Label1.Caption = UserForm14_ListBox1.Value
End Sub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34