ListBox 控件、List 属性事件ListBox control, List property example

下面的示例交换多列 ListBox 中的列。The following example swaps columns of a multicolumn ListBox. 该示例用两种方法使用 List 属性:The sample uses the List property in two ways:

  • 访问和交换 ListBox 中的各个值。To access and exchange individual values in the ListBox. 在这种用法中, List 具有赋予行和列指定值的下标。In this usage, List has subscripts to designate the row and column of a specified value.

  • 用数组中的值作为 ListBox 的初始加载值。To initially load the ListBox with values from an array. 在此用法中, List 没有下标。In this usage, List has no subscripts.

若要使用此示例,请将此示例代码复制到窗体的 Declarations 部分。To use this example, copy this sample code to the Declarations portion of a form. 确保该窗体含有一个名为"ListBox1"的 ListBox 和一个名为"CommandButton1"的 CommandButtonMake sure that the form contains a ListBox named ListBox1 and a CommandButton named CommandButton1.

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

支持和反馈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.