组合框(ComboBox)控件用于显示各种项目的下拉列表。它是用户输入项目的文本框和用户选择项目的下拉列表的组合。
下面创建一个组合框,从工具箱中拖动一个组合框(ComboBox)控件,然后将其放在窗体上。
可以从属性窗口或运行时填充列表框项目。要将项目添加到组合框,请选择组合框控件,然后转到属性窗口以获取此控件的属性。单击Items属性旁边的省略号(...
)按钮。 这将打开“字符串集合编辑器”对话框,在其中输入一行的值。
ComboBox控件的属性
以下是组合控件(ComboBox)控件的一些常用属性:
编号 | 属性 | 描述 |
---|---|---|
1 | AllowSelection |
获取一个值,该值指示列表是否允许选择列表项目。 |
2 | AutoCompleteCustomSource |
获取或设置AutoCompleteSource 属性设置为CustomSource 时使用的自定义System.Collections.Specialized.StringCollection 。 |
3 | AutoCompleteMode |
获取或设置一个选项,用于控制ComboBox 自动完成的工作方式。 |
4 | AutoCompleteSource |
获取或设置一个值,指定用于自动完成的完整字符串的来源。 |
5 | DataBindings |
获取控件的数据绑定。 |
6 | DataManager |
获取与此控件关联的CurrencyManager 。 |
7 | DataSource |
获取或设置此ComboBox 的数据源。 |
8 | DropDownHeight |
获取或设置组合框的下拉部分的高度(以像素为单位)。 |
9 | DropDownStyle |
获取或设置指定组合框样式的值。 |
10 | DropDownWidth |
获取或设置组合框的下拉部分的宽度。 |
11 | DroppedDown |
获取或设置一个值,该值指示组合框是否显示其下拉部分。 |
12 | FlatStyle |
获取或设置组合框的外观。 |
13 | ItemHeight |
获取或设置组合框中项目的高度。 |
14 | Items |
获取表示此ComboBox 中包含的项目集合的对象。 |
15 | MaxDropDownItems |
获取或设置要在组合框的下拉部分中显示的项目的最大数量。 |
16 | MaxLength |
获取或设置用户可以在组合框的可编辑区域输入的最大字符数。 |
17 | SelectedIndex |
获取或设置指定当前选定项目的索引。 |
18 | SelectedItem |
获取或设置ComboBox中当前选定的项目。 |
19 | SelectedText |
获取或设置在ComboBox的可编辑部分中选择的文本。 |
20 | SelectedValue |
获取或设置由ValueMember属性指定的成员属性的值。 |
21 | SelectionLength |
获取或设置在组合框的可编辑部分中选择的字符数。 |
22 | SelectionStart |
获取或设置组合框中选定文本的起始索引。 |
23 | Sorted |
获取或设置一个值,该值指示组合框中的项目是否已排序。 |
24 | Text |
获取或设置与此控件关联的文本。 |
ComboBox控件的方法
以下是ComboBox控件的一些常用方法:
编号 | 方法 | 描述 |
---|---|---|
1 | BeginUpdate |
防止控件绘制,直到调用EndUpdate 方法,而项目一次一个添加到组合框。 |
2 | EndUpdate |
在由BeginUpdate 方法关闭后,继续绘制组合框。 |
3 | FindString |
查找组合框中以指定为参数的字符串开头的第一个项目。 |
4 | FindStringExact |
在组合框中查找与指定字符串完全匹配的第一个项目。 |
5 | SelectAll |
选择组合框可编辑区域中的所有文本。 |
ComboBox控件的事件
以下是ComboBox控件的一些常用事件:
编号 | 事件 | 描述 |
---|---|---|
1 | DropDown |
在显示组合框的下拉部分时发生。 |
2 | DropDownClosed |
在组合框的下拉部分不再可见时发生。 |
3 | DropDownStyleChanged |
在ComboBox的DropDownStyle 属性发生更改时发生。 |
4 | SelectedIndexChanged |
在ComboBox控件的SelectedIndex 属性更改时发生。 |
5 | SelectionChangeCommitted |
在所选项目已更改并且更改显示在组合框中时发生。 |
示例
在这个例子中,使用各种项目来填充组合框,获取组合框中的选定项目,并将其显示在列表框中并对项目进行排序。
拖放组合框以存储项目,显示选定项目的列表框,用选定项目添加到列表框的四个按钮控件,填充组合框,对项目进行排序以及清除组合框列表。
添加一个可显示选定项目的标签控件。参考以下布局 -
在代码编辑器窗口中添加以下代码:
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 = "ComboBox控件示例 - xuhuhu.com"
End Sub
'选择填充到右侧ListBox - sends the selected items to the list box '
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnSend.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 btnFill.Click
ComboBox1.Items.Clear()
ComboBox1.Items.Add("周游全国")
ComboBox1.Items.Add("通过六级")
ComboBox1.Items.Add("减肥120斤")
ComboBox1.Text = "Select from..."
End Sub
'排序 - sorting the list '
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnSorted.Click
ComboBox1.Sorted = True
End Sub
'清除 - clears the list '
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnClear.Click
ComboBox1.Items.Clear()
End Sub
'显示当前选择 - displaying the selected item on the label '
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Label2.Text = ComboBox1.SelectedItem.ToString()
End Sub
End Class
当上面的代码执行并使用Microsoft Visual Studio工具栏上的“开始”按钮运行时,它将显示以下窗口:
点击各个按钮来检查每个按钮所执行的操作:
上一篇:
VB.Net基本控件
下一篇:
VB.Net对话框