VB.Net下拉式方塊(ComboBox)

2019-10-16 23:01:41

下拉式方塊(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控制元件範例 - tw511.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工具列上的「開始」按鈕執行時,它將顯示以下視窗:

點選各個按鈕來檢查每個按鈕所執行的操作: