Visual Basic 组合下拉框 ComboBox
ComboBox 控件用于显示各种项目的下拉列表。它是一个文本框(用户在其中输入项目)和下拉列表(用户从中选择项目)的组合。
让我们通过从工具箱中拖动 ComboBox 控件并将其放到表单上来创建一个组合下拉框。

您可以从 "属性" 窗口或在运行时填充列表框项目。要向组合框中添加项目,请选择组合框控件并转到此控件属性的属性窗口。单击椭圆(…)Items 属性旁边的按钮。这将打开 "字符串集合编辑器" 对话框,您可以在其中一行输入一个值。
ComboBox 控件的属性
以下是 ComboBox 控件的一些常用属性:
| 编号 | 属性 & 描述 |
|---|---|
| 1 | AllowSelection 获取一个值,该值表示列表是否允许选择列表项。 |
| 2 | AutoCompleteCustomSource 获取或设置当 AutoCompleteSourceproperty 设置为 CustomSource 时要使用的自定义 System.Collections.Specialized.StringCollection。 |
| 3 | AutoCompleteMode 获取或设置一个选项,表示该 ComboBox 控件的自动完成功能。 |
| 4 | AutoCompleteSource 获取或设置一个值,该值指定用于自动完成的完整字符串的数据源。 |
| 5 | DataBindings 获取控件的数据绑定。 |
| 6 | DataManager 获取与此控件关联的 CurrencyManager。 |
| 7 | DataSource 获取或设置此 ComboBox 的数据源。 |
| 8 | DropDownHeight 获取或设置 ComboBox 下拉部分的高度(以像素为单位)。 |
| 9 | DropDownStyle 获取或设置一个值,该值指定 ComboBox 的样式。 |
| 10 | DropDownWidth 获取或设置 ComboBox 下拉部分的宽度。 |
| 11 | DroppedDown 获取或设置一个值,该值指示 ComboBox 是否显示其下拉部分。 |
| 12 | FlatStyle 获取或设置 ComboBox 的外观。 |
| 13 | ItemHeight 获取或设置 ComboBox 中项目的高度。 |
| 14 | Items 获取一个对象,该对象表示此 ComboBox 中包含的项的集合。 |
| 15 | MaxDropDownItems 获取或设置要在 ComboBox 下拉部分中显示的最大项数。 |
| 16 | MaxLength 获取或设置用户可以在 ComboBox 的可编辑区域中输入的最大字符数。 |
| 17 | SelectedIndex 获取或设置指定当前选定项的索引。 |
| 18 | SelectedItem 获取或设置 ComboBox 中当前选定的项。 |
| 19 | SelectedText 获取或设置在 ComboBox 的可编辑部分中选择的文本。 |
| 20 | Selected值 获取或设置 ValueMember 属性指定的成员属性的值。 |
| 21 | SelectionLength 获取或设置在 ComboBox 的可编辑部分中选择的字符数。 |
| 22 | SelectionStart 获取或设置 ComboBox 中选定文本的起始索引。 |
| 23 | Sorted 获取或设置一个值,该值表示 ComboBox 的项目是否已排序。 |
| 24 | Text 获取或设置与此控件关联的文本。 |
ComboBox 控件的方法
以下是 ComboBox 控件的一些常用方法:
| 编号 | 方法名称 & 描述 |
|---|---|
| 1 | BeginUpdate 阻止控件绘制,直到调用 EndUpdate 方法,同时将项目一次添加到 ComboBox 中。 |
| 2 | EndUpdate 使用 BeginUpdate 方法关闭 ComboBox 后,继续绘制 ComboBox。 |
| 3 | FindString 在 ComboBox 中查找以指定为参数的字符串开头的第一项。 |
| 4 | FindStringExact 在 ComboBox 中查找与指定字符串完全匹配的第一项。 |
| 5 | SelectAll 选择 ComboBox 可编辑区域中的所有文本。 |
ComboBox 控件的事件
以下是 ComboBox 控件的一些事件:
| 编号 | 事件 & 描述 |
|---|---|
| 1 | DropDown 显示 ComboBox 的下拉部分时发生。 |
| 2 | DropDownClosed 当 ComboBox 的下拉部分不再可见时发生。 |
| 3 | DropDownStyleChanged 在 ComboBox 的 DropDownStyle 属性更改时发生。 |
| 4 | SelectedIndexChanged 在 ComboBox 控件的 SelectedIndex 属性更改时发生。 |
| 5 | SelectionChangeCommitted 在所选项目已更改且更改显示在 ComboBox 中时发生。 |
实例
在本例中,让我们用各种项填充 ComboBox,在 ComboBox 中获取所选项目,并在列表框中显示它们,然后对项目进行排序。
拖放一个 ComboBox 以存储项目,一个列表框以显示选定项,四个按钮控件分别用于向列表框中添加选定项、填充 ComboBox、排序项目和清除 ComboBox 列表。
添加将显示所选项目的标签控件。

在代码编辑器窗口中添加以下代码:
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"End Sub'sends the selected items to the list boxPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickIf ComboBox1.SelectedIndex > -1 ThenDim sindex As Integersindex = ComboBox1.SelectedIndexDim sitem As Objectsitem = ComboBox1.SelectedItemListBox1.Items.Add(sitem)End IfEnd Sub'populates the listPrivate Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickComboBox1.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 listPrivate Sub Button3_Click(sender As Object, e As EventArgs)ComboBox1.Sorted = TrueEnd Sub'clears the listPrivate Sub Button4_Click(sender As Object, e As EventArgs)ComboBox1.Items.Clear()End Sub'displaying the selected item on the labelPrivate Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _Handles ListBox1.SelectedIndexChangedLabel1.Text = ComboBox1.SelectedItem.ToString()End SubEnd Class
当使用 Microsoft Visual Studio 工具栏上的 开始 按钮执行并运行上述代码时,它将显示以下窗口:

单击各种按钮检查每个按钮执行的操作:
