Visual Basic 组合下拉框 ComboBoxComboBox 控件用于显示各种项目的下拉列表。它是一个文本框(用户在其中输入项目)和下拉列表(用户从中选择项目)的组合。
让我们通过从工具箱中拖动 ComboBox 控件并将其放到表单上来创建一个组合下拉框。
您可以从 "属性" 窗口或在运行时填充列表框项目。要向组合框中添加项目,请选择组合框控件并转到此控件属性的属性窗口。单击椭圆(…)Items 属性旁边的按钮。这将打开 "字符串集合编辑器" 对话框,您可以在其中一行输入一个值。
ComboBox 控件的属性以下是 ComboBox 控件的一些常用属性:
编号属性 & 描述1AllowSelection
获取一个值,该值表示列表是否允许选择列表项。
2AutoCompleteCustomSource
获取或设置当 AutoCompleteSourceproperty 设置为 CustomSource 时要使用的自定义 System.Collections.Specialized.StringCollection。
3AutoCompleteMode
获取或设置一个选项,表示该 ComboBox 控件的自动完成功能。
4AutoCompleteSource
获取或设置一个值,该值指定用于自动完成的完整字符串的数据源。
5DataBindings
获取控件的数据绑定。
6DataManager
获取与此控件关联的 CurrencyManager。
7DataSource
获取或设置此 ComboBox 的数据源。
8DropDownHeight
获取或设置 ComboBox 下拉部分的高度(以像素为单位)。
9DropDownStyle
获取或设置一个值,该值指定 ComboBox 的样式。
10DropDownWidth
获取或设置 ComboBox 下拉部分的宽度。
11DroppedDown
获取或设置一个值,该值指示 ComboBox 是否显示其下拉部分。
12FlatStyle
获取或设置 ComboBox 的外观。
13ItemHeight
获取或设置 ComboBox 中项目的高度。
14Items
获取一个对象,该对象表示此 ComboBox 中包含的项的集合。
15MaxDropDownItems
获取或设置要在 ComboBox 下拉部分中显示的最大项数。
16MaxLength
获取或设置用户可以在 ComboBox 的可编辑区域中输入的最大字符数。
17SelectedIndex
获取或设置指定当前选定项的索引。
18SelectedItem
获取或设置 ComboBox 中当前选定的项。
19SelectedText
获取或设置在 ComboBox 的可编辑部分中选择的文本。
20Selected值
获取或设置 ValueMember 属性指定的成员属性的值。
21SelectionLength
获取或设置在 ComboBox 的可编辑部分中选择的字符数。
22SelectionStart
获取或设置 ComboBox 中选定文本的起始索引。
23Sorted
获取或设置一个值,该值表示 ComboBox 的项目是否已排序。
24Text
获取或设置与此控件关联的文本。
ComboBox 控件的方法以下是 ComboBox 控件的一些常用方法:
编号方法名称 & 描述1BeginUpdate
阻止控件绘制,直到调用 EndUpdate 方法,同时将项目一次添加到 ComboBox 中。
2EndUpdate
使用 BeginUpdate 方法关闭 ComboBox 后,继续绘制 ComboBox。
3FindString
在 ComboBox 中查找以指定为参数的字符串开头的第一项。
4FindStringExact
在 ComboBox 中查找与指定字符串完全匹配的第一项。
5SelectAll
选择 ComboBox 可编辑区域中的所有文本。
ComboBox 控件的事件以下是 ComboBox 控件的一些事件:
编号事件 & 描述1DropDown
显示 ComboBox 的下拉部分时发生。
2DropDownClosed
当 ComboBox 的下拉部分不再可见时发生。
3DropDownStyleChanged
在 ComboBox 的 DropDownStyle 属性更改时发生。
4SelectedIndexChanged
在 ComboBox 控件的 SelectedIndex 属性更改时发生。
5SelectionChangeCommitted
在所选项目已更改且更改显示在 ComboBox 中时发生。
实例在本例中,让我们用各种项填充 ComboBox,在 ComboBox 中获取所选项目,并在列表框中显示它们,然后对项目进行排序。
拖放一个 ComboBox 以存储项目,一个列表框以显示选定项,四个按钮控件分别用于向列表框中添加选定项、填充 ComboBox、排序项目和清除 ComboBox 列表。
添加将显示所选项目的标签控件。
在代码编辑器窗口中添加以下代码:
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" End Sub 'sends the selected items to the list box Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If ComboBox1.SelectedIndex > -1 Then Dim sindex As Integer sindex = ComboBox1.SelectedIndex Dim sitem As Object sitem = ComboBox1.SelectedItem ListBox1.Items.Add(sitem) End If End Sub 'populates the list Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click ComboBox1.Items.Clear() ComboBox1.Items.Add("Safety") ComboBox1.Items.Add("Security") ComboBox1.Items.Add("Governance") ComboBox1.Items.Add("Good Music") ComboBox1.Items.Add("Good Movies") ComboBox1.Items.Add("Good Books") ComboBox1.Items.Add("Education") ComboBox1.Items.Add("Roads") ComboBox1.Items.Add("Health") ComboBox1.Items.Add("Food for all") ComboBox1.Items.Add("Shelter for all") ComboBox1.Items.Add("Industrialisation") ComboBox1.Items.Add("Peace") ComboBox1.Items.Add("Liberty") ComboBox1.Items.Add("Freedom of Speech") ComboBox1.Text = "Select from..." End Sub 'sorting the list Private Sub Button3_Click(sender As Object, e As EventArgs) ComboBox1.Sorted = True End Sub 'clears the list Private Sub Button4_Click(sender As Object, e As EventArgs) ComboBox1.Items.Clear() End Sub 'displaying the selected item on the label Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _ Handles ListBox1.SelectedIndexChanged Label1.Text = ComboBox1.SelectedItem.ToString() End SubEnd Class当使用 Microsoft Visual Studio 工具栏上的 开始 按钮执行并运行上述代码时,它将显示以下窗口:
单击各种按钮检查每个按钮执行的操作: