簡體   English   中英

如何在 VB.NET 中使 combobox 自動完成

[英]How to make combobox autocomplete in VB.NET

我如何調整下面的代碼,使 ComboBox 自動完成?

Private Sub PopulateComboBox()
    Dim query As String = "SELECT DISTINCT PNM FROM GSD UNION SELECT DISTINCT PNM FROM GSG ORDER BY PNM"
    Try
        Using con As OleDbConnection = New OleDbConnection(cn)
            Using sda As OleDbDataAdapter = New OleDbDataAdapter(query, con)
                'Fill the DataTable with records from Table.
                Dim dt As DataTable = New DataTable()
                sda.Fill(dt)

                'Insert the Default Item to DataTable.
                Dim row As DataRow = dt.NewRow()

                row(0) = ""
                dt.Rows.InsertAt(row, 0)

                'Assign DataTable as DataSource.
                ComboBox1.DataSource = dt
                ComboBox1.DisplayMember = "PNM"
                ComboBox1.ValueMember = "PNM"
            End Using
        End Using
    Catch myerror As OleDbException
        MessageBox.Show("Error: " & myerror.Message)
    Finally
    End Try
End Sub

Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
    fillDataGridView1()
    fillDataGridView2()
End Sub

這取決於你到底想要什么和需要什么。

您只能在代碼中更改自動完成屬性,當 FORM_LOAD 事件進行時,更改將不會生效。

這是操縱自動完成的所有三種可能性

AutoCompleteMode決定了你想要什么樣的自動完成。

rest 是當您有自動完成的額外來源時

    Dim source As New AutoCompleteStringCollection()
    source.AddRange(New String() _
                    {
                        "one",
                        "two",
                        "three",
                        "Four"
                    })
    With ComboBox1
        .AutoCompleteCustomSource = source
        .AutoCompleteMode = AutoCompleteMode.SuggestAppend
        .AutoCompleteSource = AutoCompleteSource.CustomSource
    End With

您必須使用這些項目創建一個 AutoCompleteStringCollection,然后將其添加為 AutoCompleteCustomSource。

必須創建一個字符串數組。 要從數據庫執行此操作,我建議使用實體框架或 ORM package 例如 Dapper。

這是一個例子。 首先將Dapper Nuget Package 安裝到項目中。

Imports Dapper

Private connectionString = "Server=xxxxx;Database=xxxx;Trusted_Connection=True;"

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ComboBox1.AutoCompleteCustomSource = AutoSuggestList()
End Sub

Private Function AutoSuggestList() As AutoCompleteStringCollection
    Dim collection As New AutoCompleteStringCollection
    Using connection As New SqlConnection(connectionString)
        collection.AddRange(connection.Query(Of String)("SELECT DISTINCT PNM FROM GSD UNION SELECT DISTINCT PNM FROM GSG ORDER BY PNM").ToArray)
    End Using
    Return collection
End Function

或者異步加載,你可以使用:

Private Async Function LoadAutoSuggestListAsync() As Task(Of AutoCompleteStringCollection)
    Dim collection As New AutoCompleteStringCollection
    Using connection As New SqlConnection(connectionString)
        Dim list = Await connection.QueryAsync(Of String)(sqlText)
        collection.AddRange(list.ToArray)
    End Using
    Return collection
End Function

Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ComboBox1.AutoCompleteCustomSource = Await LoadAutoSuggestListAsync()
End Sub

此外,構建列表不限於加載事件。

順便說一句,Microsoft 建議不要使用 System.Data.OleDb 庫,因為它是較舊的技術。 我推薦使用 System.Data.SqlClient 庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM