Visual Basic 2008 中 Listbox列表框使用方法

属性:

(1)selectedindex 当前选定项的索引值(0开始)

(2)selecteditem 当前选定项

(3)items 列表框的集合

2、Items的属性和方法

(1)count 集合中项个数

(2)Add 添加一个项

(3)Clear 清空所有项

(4)Contains 确定指定项是否在集合中

(5)RemoveAt 删除集合中指定索引处的项

例:

Public Class Form2

Private Sub 载入数据_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 载入数据.Click
Dim i As Integer
For i = 1 To 10
ListBox1.Items.Add(i)
Next
End Sub


Private Sub ListBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.Click
MsgBox("当前选中项的索引值是:" & ListBox1.SelectedIndex)
MsgBox("当前选中项为:" & ListBox1.SelectedItem)
End Sub

Private Sub 显示所有数据_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 显示所有数据.Click
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
MsgBox(ListBox1.Items(i))
Next
End Sub

Private Sub 删除选定项_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除选定项.Click
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)

End Sub
End Class

你可能感兴趣的:(listbox)