目录

ListBox

ListBox表示一个Windows控件,用于向用户显示项目列表。 用户可以从列表中选择项目。 它允许程序员通过使用属性窗口或在运行时在设计时添加项目。

让我们通过从工具箱中拖动ListBox控件并将其放在表单上来创建一个列表框。

VB.Net列表框

您可以从属性窗口或运行时填充列表框项。 要将项添加到ListBox,请选择ListBox控件并转到属性窗口,以获取此控件的属性。 单击Items属性旁边的省略号(...)按钮。 这将打开“字符串集合编辑器”对话框,您可以在其中输入一行中的值。

ListBox控件的属性

以下是ListBox控件的一些常用属性 -

Sr.No. 财产和描述
1

AllowSelection

获取一个值,该值指示ListBox当前是否允许选择列表项。

2

BorderStyle

获取或设置列表框周围绘制的边框类型。

3

ColumnWidth

获取设置多列列表框中列的宽度。

4

HorizontalExtent

获取或设置列表框的水平滚动区域。

5

HorizontalScrollBar

获取或设置指示水平滚动条是否显示在列表框中的值。

6

ItemHeight

获取或设置列表框中项目的高度。

7

Items

获取列表框的项目。

8

MultiColumn

获取或设置一个值,该值指示列表框是否支持多列。

9

ScrollAlwaysVisible

获取或设置一个值,该值指示是否始终显示垂直滚动条。

10

SelectedIndex

获取或设置列表框中当前所选项的从零开始的索引。

11

SelectedIndices

获取一个集合,其中包含列表框中所有当前所选项目的从零开始的索引。

12

SelectedItem

获取或设置列表框中当前选定的项目。

13

SelectedItems

获取包含列表框中当前所选项的集合。

14

SelectedValue

获取或设置ValueMember属性指定的成员属性的值。

15

SelectionMode

获取或设置在列表框中选择项的方法。 这个属性有价值 -

  • None
  • One
  • MultiSimple
  • MultiExtended

16

Sorted

获取或设置一个值,该值指示列表框中的项是否按字母顺序排序。

17

Text

获取或搜索列表框中当前所选项的文本。

18

TopIndex

获取或设置列表框的第一个可见项的索引。

ListBox控件的方法

以下是ListBox控件的一些常用方法 -

Sr.No. 方法名称和描述
1

BeginUpdate

阻止控件绘制,直到调用EndUpdate方法,同时将项目一次添加到ListBox。

2

ClearSelected

取消选择ListBox中的所有项目。

3

EndUpdate

在BeginUpdate方法关闭后继续绘制列表框。

4

FindString

查找ListBox中以指定为参数的字符串开头的第一个项目。

5

FindStringExact

查找ListBox中与指定字符串完全匹配的第一项。

6

GetSelected

返回一个值,指示是否选择了指定的项。

7

SetSelected

选择或清除ListBox中指定项的选择。

8

OnSelectedIndexChanged

引发SelectedIndexChanged事件。

8

OnSelectedValueChanged

引发SelectedValueChanged事件。

ListBox控件的事件

以下是ListBox控件的一些常用事件 -

Sr.No. 活动和描述
1

Click

选择列表框时发生。

2

SelectedIndexChanged

更改列表框的SelectedIndex属性时发生。

有关ListBox控件的属性,方法和事件的详细列表,请参阅Microsoft文档。

例子1 (Example 1)

在下面的示例中,让我们在设计时添加一个列表框,并在运行时在其上添加项。

采取以下步骤 -

  • 在窗体上拖放两个标签,一个按钮和一个ListBox控件。

  • 设置第一个标签的Text属性以提供标题“为高等教育选择您最喜欢的目的地”。

  • 设置第二个标签的Text属性以提供标题“Destination”。 当用户选择列表中的项目时,此标签上的文本将在运行时更改。

  • 单击列表框和按钮控件以在代码编辑器中添加以下代码。

Public Class Form1
   Private 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 Sub
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      MsgBox("You have selected " + ListBox1.SelectedItem.ToString())
   End Sub
   Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) 
      Handles ListBox1.SelectedIndexChanged
      Label2.Text = ListBox1.SelectedItem.ToString()
   End Sub
End Class

当使用Microsoft Visual Studio工具栏上的“开始”按钮执行并运行上述代码时,它将显示以下窗口 -

结果表格

当用户选择目的地时,第二个标签中的文本会发生变化 -

结果表格

单击“选择”按钮将显示一个消息框,其中包含用户的选择 -

列表示例对话框

例子2 (Example 2)

在此示例中,我们将填充包含项目的列表框,检索列表框中的项目总数,对列表框进行排序,删除一些项目并清除整个列表框。

设计表格 -

列表框示例2

在代码编辑器窗口中添加以下代码 -

Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Set the caption bar text of the form.  
      Me.Text = "tutorialspont.com"
      ' creating multi-column and multiselect list box
      ListBox1.MultiColumn = True
      ListBox1.SelectionMode = SelectionMode.MultiExtended
   End Sub
   'populates the list
   Private Sub Button1_Click_1(sender As Object, e As EventArgs) _
      Handles Button1.Click
      ListBox1.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 list
   Private Sub Button2_Click(sender As Object, e As EventArgs) _
      Handles Button2.Click
      ListBox1.Sorted = True
   End Sub
   'clears the list
   Private Sub Button3_Click(sender As Object, e As EventArgs) _
      Handles Button3.Click
      ListBox1.Items.Clear()
   End Sub
   'removing the selected item
   Private Sub Button4_Click(sender As Object, e As EventArgs) _
          Handles Button4.Click
      ListBox1.Items.Remove(ListBox1.SelectedItem.ToString)
   End Sub
   'counting the numer of items
   Private Sub Button5_Click(sender As Object, e As EventArgs) _
      Handles Button5.Click
      Label1.Text = ListBox1.Items.Count
   End Sub
   'displaying the selected item on the third label
   Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
      Handles ListBox1.SelectedIndexChanged
      Label3.Text = ListBox1.SelectedItem.ToString()
   End Sub
End Class

当使用Microsoft Visual Studio工具栏上的“开始”按钮执行并运行上述代码时,它将显示以下窗口 -

结果表单列表示例2

填写清单并检查其他按钮的工作情况 -

结果表单列表示例2
↑回到顶部↑
WIKI教程 @2018