組合框(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對話框