ListBox 控件、List 属性事件

下面的示例交换多列 ListBox 中的列。 该示例用两种方法使用 List 属性:

  • 访问和交换 ListBox 中的各个值。 在这种用法中, List 具有赋予行和列指定值的下标。

  • 用数组中的值作为 ListBox 的初始加载值。 在此用法中, List 没有下标。

若要使用此示例,请将此示例代码复制到窗体的 Declarations 部分。 确保该窗体含有一个名为"ListBox1"的 ListBox 和一个名为"CommandButton1"的 CommandButton

Dim MyArray(6, 3) 
'Array containing column values for ListBox. 
 
Private Sub UserForm_Initialize() 
 Dim i As Single 
 
 ListBox1.ColumnCount = 3 
'This list box contains 3 data columns 
 
 'Load integer values MyArray 
 For i = 0 To 5 
 MyArray(i, 0) = i 
 MyArray(i, 1) = Rnd 
 MyArray(i, 2) = Rnd 
 Next i 
 
 'Load ListBox1 
 ListBox1.List() = MyArray 
 
End Sub
Private Sub CommandButton1_Click() 
' Exchange contents of columns 1 and 3 
 
 Dim i As Single 
 Dim Temp As Single 
 
 For i = 0 To 5 
 Temp = ListBox1.List(i, 0) 
 ListBox1.List(i, 0) = ListBox1.List(i, 2) 
 ListBox1.List(i, 2) = Temp 
 Next i 
End Sub

支持和反馈

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