Visual Basic 列表框 ListBox |
您所在的位置:网站首页 › vb按钮名称怎么改 › Visual Basic 列表框 ListBox |
Visual Basic 列表框 ListBox ListBox 列表框表示一个 Windows 控件,用于向用户显示项目列表。用户可以从列表中选择项。它可以让程序员在设计时通过使用属性窗口或在运行时添加项。 让我们通过从工具箱中拖动 ListBox 控件并将其放到表单上来创建一个列表框。 您可以从 "属性" 窗口或在运行时填充列表框项目。要向列表框中添加项目,请选择列表框控件,然后转到属性窗口,以获取该控件的属性。单击 Items 属性旁边的省略号(…)按钮。这将打开 "字符串集合编辑器" 对话框,您可以在其中一行输入一个值。 ListBox 控件的属性以下是 ListBox 控件的一些常用属性: 编号属性 & 描述1AllowSelection 获取一个值,该值表示列表框当前是否允许选择列表项。 2BorderStyle 获取或设置围绕列表框绘制的边框类型。 3ColumnWidth 设置多列列表框中列的宽度。 4HorizontalExtent 获取或设置列表框的水平滚动区域。 5HorizontalScrollBar 获取或设置一个值,该值表示是否在列表框中显示水平滚动条。 6ItemHeight 获取或设置列表框中项目的高度。 7Items 获取列表框的项。 8MultiColumn 获取或设置一个值,该值表示列表框是否支持多列。 9ScrollAlwaysVisible 获取或设置一个值,该值表示是否始终显示垂直滚动条。 10SelectedIndex 获取或设置列表框中当前选定项的从零开始的索引。 11SelectedIndices 获取一个集合,该集合包含列表框中所有当前选定项的从 0 开始的索引。 12SelectedItem 获取或设置列表框中当前选定的项。 13SelectedItems 获取包含列表框中当前选定项的集合。 14Selected值 获取或设置 ValueMember 属性指定的成员属性的值。 15SelectionMode 获取或设置在列表框中选择项的方法。此属性的有有效值为: NoneOneMultiSimpleMultiExtended16Sorted 获取或设置一个值,该值表示列表框中的项是否按字母顺序排序。 17Text 获取或搜索列表框中当前选定项的文本。 18TopIndex 获取或设置列表框第 1 个可见项的索引。 ListBox 控件的方法以下是 ListBox 控件的一些常用属性: 编号方法名称 & 描述1BeginUpdate 在调用 EndUpdate 方法之前,防止控件绘制,同时将项目一次添加到列表框中。 2ClearSelected 取消选择列表框中的所有项。 3EndUpdate 使用 BeginUpdate 方法关闭列表框后,继续绘制该列表框。 4FindString 查找列表框中以指定为参数的字符串开头的第一项。 5FindStringExact 在列表框中查找与指定字符串完全匹配的第一项。 6GetSelected 返回一个值,该值表示是否选择了指定的项。 7SetSelected 选择或清除列表框中指定项目的选择。 8OnSelectedIndexChanged 触发 SelectedIndexChanged 事件。 8OnSelectedValueChanged 触发 OnSelectedValueChanged 事件。 ListBox 控件的事件以下是 ListBox 控件的一些常用事件: 编号事件 & 描述1Click 选择列表框时发生。 2SelectedIndexChanged 更改列表框的 SelectedIndex 属性时发生。 有关 ListBox 控件的属性、方法和事件的详细列表,请参考 Microsoft 文档。 实例 1在下面的实例中,让我们在设计时添加一个列表框,并在运行时在其上添加项目。 使用以下步骤: 在表单上拖放两个标签、一个按钮和一个列表框控件。设置第一个标签的文本属性,以提供标题 "Choose your favourite destination for higher studies"。设置第二个标签的 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 SubEnd Class当使用 Microsoft Visual Studio 工具栏上的开始按钮执行并运行上述代码时,它将显示以下窗口: 当用户选择目的地时,第二个标签中的文本会更改: 单击Select(选择)按钮将显示一个包含用户选择的消息框: 实例 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 = "cankaoshouce.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 SubEnd Class当使用 Microsoft Visual Studio 工具栏上的开始按钮执行并运行上述代码时,它将显示以下窗口: 填写列表并检查其他按钮的运行情况: |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |