如何使我的组合框自动完成

问题描述:

嗨.我的表单上有一个comboBox,其设置为以下代码:

Hi. I have a comboBox on my form which is set to following code:

dtFetch = retrieveFull("ProductBasicInfo")

            cmbSProdID.DisplayMember = "ProdName"
            cmbSProdID.ValueMember = "ProdID"
            cmbSProdID.DataSource = dtFetch

其中dtFetch是一个简单的DataTable,retrieveFull是一个函数:

Where dtFetch is a simple DataTable and retrieveFull is a function:

Public Function retrieveFull(ByVal tblName As String) As DataTable
        Try
            If dsSql.Tables.Contains(tblName) Then
                dsSql.Tables.Remove(tblName)
            End If

            cmdSql1.CommandText = "select * from " & tblName
            cmdSql1.Connection = Connect()
            daSql.SelectCommand = cmdSql1
            daSql.Fill(dsSql, tblName)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Return dsSql.Tables(tblName)
    End Function

现在.我想使我的组合框自动完成.因为整个产品列表都已加载到此组合中,所以当用户单击它时,长长的列表出现在他/她的身上,看起来很奇怪.因此,我要使其自动完成,如他所写 M,因此M的所有产品都会出现在列表中,当他在M之后按O时,会出现所有以MO开头的产品.为此,请协助我.谢谢.如果需要任何其他信息,那么我将提供它.


Now. I want to make my combobox AutoComplete. Because the entire list of Products is being loaded into this Combo, so when user clicks on it so a lengthy list appears to him/ her, which looks so weired. Therefore, i want to make it autoComplete that as he writes M so all products of M appears into list and when he press O after M so all products starting with MO will appear. Please assist me for this. Thanks. If any other info needed so i will provide it.


你好,

下面的MSDN文章附有源代码,可以进行编译,展示了如何使用后端数据库中的数据为AutoComplete设置文本框.

The following MSDN article complete with source, ready to compile shows how to setup a TextBox for AutoComplete using data from a backend database.

下载项目,打开表单,将ComboBox添加到名称为ComboBox1的表单中.将以下代码添加到Form load事件中.

Download the project, open the form, add a ComboBox to the form with the name of ComboBox1. Add the following code to the Form load event.

ComboBox1.DisplayMember = "FirstName"
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
ComboBox1.AutoCompleteCustomSource = LoadFemaleNames()

我在其中添加了组合框的屏幕截图. DataGridView只是用来显示可以使用哪些数据

Screenshot where I added a ComboBox. The DataGridView is there just to show what data can be used