ListBox 控件显示项目列表,从其中可以选择一项或多项。如果项目总数超过了可显示的项目数,就自动在 ListBox 控件上添加滚动条。
如果未选定项目,则 ListIndex 属性值是 -1。列表的第一项是 ListIndex 0,ListCount 属性值总是比最大的 ListIndex 值大 1。
语法
ListBox
说明
使用 AddItem 或者 RemoveItem 方法可以添加或者删除 ListBox 控件中的项目。对 List、ListCount 和 ListIndex 属性进行设置就可以访问 ListBox 中的项目。也可以在设计时使用 List 属性在列表中增加项目。
实例
Dim Entry, I, Msg ' 声明变量。
Msg = "Choose OK to add 100 items to your list box."
MsgBox Msg ' 显示信息。
For I = 1 To 100 ' 计数值从 1 到 100。
Entry = "Entry " & I ' 创建输入项。
List1.AddItem Entry ' 添加该输入项。
Next I
Msg = "Choose OK to remove every other entry."
MsgBox Msg ' 显示信息。
For I = 1 To 50 ' 确定如何
List1.RemoveItem I ' 每隔一项
Next I ' 删除。
Msg = "Choose OK to remove all items from the list box."
MsgBox Msg ' 显示信息。
List1.Clear ' 清除列表框。