【问题标题】:How to make combobox autocomplete in VB.NET如何在 VB.NET 中使组合框自动完成
【发布时间】:2022-12-08 21:21:01
【问题描述】:

亲爱的所有大师,

如何在 VB.NET 中使组合框自动完成?

如何调整下面的代码以便我创建组合框自动完成。请推荐最佳解决方案。

谢谢

 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

【问题讨论】:

    标签: vb.net datatable datagridview combobox oledb


    【解决方案1】:

    这取决于你到底想要什么和需要什么。

    这是操纵自动完成的所有三种可能性

    AutoCompleteMode 决定了你想要什么样的自动完成。

    剩下的就是当你有自动完成的额外来源时

        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
    

    【讨论】:

    • 谢谢你的回复,但我试过你的代码我在组合框中试过它没有出现自动完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多