指定要向用户显示的多列列表框中的列

以下示例使用 TextColumn 属性标识 ListBox 中为其 Text 属性提供数据的数据列。 此示例将 ListBox 设置为文本列。 从 ListBox 中选择条目时, TextColumn 中的值将显示在 TextBox 中

此示例还演示如何使用 AddItem 方法和 List 属性加载多列 ListBox

若要使用本示例,请将此示例代码复制到窗体的"脚本编辑器"中。 若要运行本代码,需要打开该窗体,以便激活 Open 事件。 确保该窗体包含:

  • 一个名为"ListBox1"的 ListBox

  • 一个名为"TextBox1"的 TextBox

Dim ListBox1 
Dim TextBox1 
 
Sub Item_Open() 
 Set ListBox1 = Item.GetInspector.ModifiedFormPages.Item("P.2").Controls("ListBox1") 
 Set TextBox1 = Item.GetInspector.ModifiedFormPages.Item("P.2").Controls("TextBox1") 
 
 ListBox1.ColumnCount = 3 
 
 ListBox1.AddItem "Row 1, Col 1" 
 ListBox1.List(0, 1) = "Row 1, Col 2" 
 ListBox1.List(0, 2) = "Row 1, Col 3" 
 
 ListBox1.AddItem "Row 2, Col 1" 
 ListBox1.List(1, 1) = "Row 2, Col 2" 
 ListBox1.List(1, 2) = "Row 2, Col 3" 
 
 ListBox1.AddItem "Row 3, Col 1" 
 ListBox1.List(2, 1) = "Row 3, Col 2" 
 ListBox1.List(2, 2) = "Row 3, Col 3" 
 
 ListBox1.TextColumn = 3 
 
End Sub 
 
Sub ListBox1_Click() 
 TextBox1.Text = ListBox1.Text 
End Sub

支持和反馈

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