Visual Basic 列表框 ListBox
ListBox 列表框表示一个 Windows 控件,用于向用户显示项目列表。用户可以从列表中选择项。它可以让程序员在设计时通过使用属性窗口或在运行时添加项。
让我们通过从工具箱中拖动 ListBox 控件并将其放到表单上来创建一个列表框。

您可以从 "属性" 窗口或在运行时填充列表框项目。要向列表框中添加项目,请选择列表框控件,然后转到属性窗口,以获取该控件的属性。单击 Items 属性旁边的省略号(…)按钮。这将打开 "字符串集合编辑器" 对话框,您可以在其中一行输入一个值。
ListBox 控件的属性
以下是 ListBox 控件的一些常用属性:
| 编号 | 属性 & 描述 |
|---|---|
| 1 | AllowSelection 获取一个值,该值表示列表框当前是否允许选择列表项。 |
| 2 | BorderStyle 获取或设置围绕列表框绘制的边框类型。 |
| 3 | ColumnWidth 设置多列列表框中列的宽度。 |
| 4 | HorizontalExtent 获取或设置列表框的水平滚动区域。 |
| 5 | HorizontalScrollBar 获取或设置一个值,该值表示是否在列表框中显示水平滚动条。 |
| 6 | ItemHeight 获取或设置列表框中项目的高度。 |
| 7 | Items 获取列表框的项。 |
| 8 | MultiColumn 获取或设置一个值,该值表示列表框是否支持多列。 |
| 9 | ScrollAlwaysVisible 获取或设置一个值,该值表示是否始终显示垂直滚动条。 |
| 10 | SelectedIndex 获取或设置列表框中当前选定项的从零开始的索引。 |
| 11 | SelectedIndices 获取一个集合,该集合包含列表框中所有当前选定项的从 0 开始的索引。 |
| 12 | SelectedItem 获取或设置列表框中当前选定的项。 |
| 13 | SelectedItems 获取包含列表框中当前选定项的集合。 |
| 14 | Selected值 获取或设置 ValueMember 属性指定的成员属性的值。 |
| 15 | SelectionMode 获取或设置在列表框中选择项的方法。此属性的有有效值为:
|
| 16 | Sorted 获取或设置一个值,该值表示列表框中的项是否按字母顺序排序。 |
| 17 | Text 获取或搜索列表框中当前选定项的文本。 |
| 18 | TopIndex 获取或设置列表框第 1 个可见项的索引。 |
ListBox 控件的方法
以下是 ListBox 控件的一些常用属性:
| 编号 | 方法名称 & 描述 |
|---|---|
| 1 | BeginUpdate 在调用 EndUpdate 方法之前,防止控件绘制,同时将项目一次添加到列表框中。 |
| 2 | ClearSelected 取消选择列表框中的所有项。 |
| 3 | EndUpdate 使用 BeginUpdate 方法关闭列表框后,继续绘制该列表框。 |
| 4 | FindString 查找列表框中以指定为参数的字符串开头的第一项。 |
| 5 | FindStringExact 在列表框中查找与指定字符串完全匹配的第一项。 |
| 6 | GetSelected 返回一个值,该值表示是否选择了指定的项。 |
| 7 | SetSelected 选择或清除列表框中指定项目的选择。 |
| 8 | OnSelectedIndexChanged 触发 SelectedIndexChanged 事件。 |
| 8 | OnSelectedValueChanged 触发 OnSelectedValueChanged 事件。 |
ListBox 控件的事件
以下是 ListBox 控件的一些常用事件:
| 编号 | 事件 & 描述 |
|---|---|
| 1 | Click 选择列表框时发生。 |
| 2 | SelectedIndexChanged 更改列表框的 SelectedIndex 属性时发生。 |
有关 ListBox 控件的属性、方法和事件的详细列表,请参考 Microsoft 文档。
实例 1
在下面的实例中,让我们在设计时添加一个列表框,并在运行时在其上添加项目。
使用以下步骤:
- 在表单上拖放两个标签、一个按钮和一个列表框控件。
- 设置第一个标签的文本属性,以提供标题 "Choose your favourite destination for higher studies"。
- 设置第二个标签的
Text属性以提供标题 "Destination"。当用户选择列表中的项目时,此标签上的文本将在运行时更改。 - 单击列表框和按钮控件在代码编辑器中添加以下代码。
Public Class Form1Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load' Set the caption bar text of the form.Me.Text = "tutorialspont.com"ListBox1.Items.Add("Canada")ListBox1.Items.Add("USA")ListBox1.Items.Add("UK")ListBox1.Items.Add("Japan")ListBox1.Items.Add("Russia")ListBox1.Items.Add("China")ListBox1.Items.Add("India")End SubPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickMsgBox("You have selected " + ListBox1.SelectedItem.ToString())End SubPrivate Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)Handles ListBox1.SelectedIndexChangedLabel2.Text = ListBox1.SelectedItem.ToString()End SubEnd Class
当使用 Microsoft Visual Studio 工具栏上的开始按钮执行并运行上述代码时,它将显示以下窗口:

当用户选择目的地时,第二个标签中的文本会更改:

单击Select(选择)按钮将显示一个包含用户选择的消息框:

实例 2
在本例中,我们将用项目填充列表框,检索列表框中的项目总数,对列表框进行排序,删除一些项并清除整个列表框。
设计表单:

在代码编辑器窗口中添加以下代码:
Public Class Form1Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load' Set the caption bar text of the form.Me.Text = "cankaoshouce.com"' creating multi-column and multiselect list boxListBox1.MultiColumn = TrueListBox1.SelectionMode = SelectionMode.MultiExtendedEnd Sub'populates the listPrivate Sub Button1_Click_1(sender As Object, e As EventArgs) _Handles Button1.ClickListBox1.Items.Add("Safety")ListBox1.Items.Add("Security")ListBox1.Items.Add("Governance")ListBox1.Items.Add("Good Music")ListBox1.Items.Add("Good Movies")ListBox1.Items.Add("Good Books")ListBox1.Items.Add("Education")ListBox1.Items.Add("Roads")ListBox1.Items.Add("Health")ListBox1.Items.Add("Food for all")ListBox1.Items.Add("Shelter for all")ListBox1.Items.Add("Industrialisation")ListBox1.Items.Add("Peace")ListBox1.Items.Add("Liberty")ListBox1.Items.Add("Freedom of Speech")End Sub'sorting the listPrivate Sub Button2_Click(sender As Object, e As EventArgs) _Handles Button2.ClickListBox1.Sorted = TrueEnd Sub'clears the listPrivate Sub Button3_Click(sender As Object, e As EventArgs) _Handles Button3.ClickListBox1.Items.Clear()End Sub'removing the selected itemPrivate Sub Button4_Click(sender As Object, e As EventArgs) _Handles Button4.ClickListBox1.Items.Remove(ListBox1.SelectedItem.ToString)End Sub'counting the numer of itemsPrivate Sub Button5_Click(sender As Object, e As EventArgs) _Handles Button5.ClickLabel1.Text = ListBox1.Items.CountEnd Sub'displaying the selected item on the third labelPrivate Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _Handles ListBox1.SelectedIndexChangedLabel3.Text = ListBox1.SelectedItem.ToString()End SubEnd Class
当使用 Microsoft Visual Studio 工具栏上的开始按钮执行并运行上述代码时,它将显示以下窗口:

填写列表并检查其他按钮的运行情况:
