none
Displaying the list of Combo Box during writing RRS feed

  • Question

  • Hi Everyone,

    I struggled with many snippets and with this code 

    Private AllNames As New DataTable 
    Private Sub cmbProductName_KeyDown(sender As Object, e As KeyEventArgs) Handles cmbProductName.KeyDown
            
            If e.KeyCode = Keys.Enter Then
               
                Dim cn As New OleDbConnection(cs)
                cn.Open()
                Dim cmd As New OleDbCommand("select ProductName from Product", cn)
                Dim dadpter As New OleDbDataAdapter(cmd)
                Dim dset As New DataSet
                dadpter.Fill(dset, "Product")
    
                Dim dtname As New DataTable
                dtname = dset.Tables("Product")
    
                Dim rows As DataRow() = dtname.Select(String.Format("ProductName LIKE '%{0}%'", cmbProductName.Text)) 
    
                AllNames = dtname.Clone()
    
                For Each r As DataRow In rows
                    AllNames.ImportRow(r)
                Next
    
                cmbProductName.DataSource = Nothing
                cmbProductName.DataSource = AllNames.DefaultView
                cmbProductName.DisplayMember = "ProductName"
    
                cn.Close()
            End If
        End Sub

    and finally i made it works with simple modifications , I got it from here ....... Now i can write any letter of the word(from the first, middle or last) in the combo box to get the filter of product name, for example (product = Scoda) if i wrote 'od' every words has these letters appear in the combobox list after i click its arrow but i need it appears in the same time. 

    My question is ......

    How can i display the list of combobox during write any letters on it in order to choose product from the list ??????

    Thanks in advance ...


    Regards From Amr_Aly

    Tuesday, December 18, 2018 8:25 PM

Answers

  • Finally I got it ................

    It's so simple to make it work ..................

    This is for any one search for such a problem .........

    Add this line in cmbProductName_TextChanged 

    Me.cmbProductName.DroppedDown = True "

    That's it 

    Thank you Alex for your time 


    Regards From Amr_Aly

    • Marked as answer by Amr_Aly Saturday, December 22, 2018 8:07 PM
    Saturday, December 22, 2018 8:07 PM

All replies

  • The only one I know of is demonstrated here, in this case using a large list that can do a contains autocomplete. The actual control is in C# but used in VB.NET. It may not suite your data source "as is" but works extremely well.

    https://1drv.ms/u/s!AtGAgKKpqdWjjG1UDQutKBOIxHTQ

     

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, December 19, 2018 3:36 AM
    Moderator
  • Hi,

    try the way,

     Dim dtname As New DataTable
        Dim AllNames As DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim cn As New OleDbConnection(cs)
                cn.Open()
                Dim cmd As New OleDbCommand("select ProductName from Product", cn)
                Dim dadpter As New OleDbDataAdapter(cmd)
                Dim dset As New DataSet
                dadpter.Fill(dset, "Product")
    
    
            dtname = dset.Tables("Product")
    
    
        End Sub
    
    
        Private Sub cmbProductName_TextChanged(sender As Object, e As KeyPressEventArgs) Handles cmbProductName_TextChanged
            Dim rows As DataRow() = dtname.Select(String.Format("ProductName LIKE '%{0}%'", cmbProductName.Text))
    
            AllNames = dtname.Clone()
    
            For Each r As DataRow In rows
                AllNames.ImportRow(r)
            Next
    
        End Sub
    
        Private Sub cmbProductName_DropDown(sender As Object, e As EventArgs) Handles cmbProductName_DropDown
            cmbProductName.DataSource = AllNames.DefaultView
            cmbProductName.DisplayMember = "ProductName"
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 19, 2018 3:39 AM
  • The only one I know of is demonstrated here, in this case using a large list that can do a contains autocomplete. The actual control is in C# but used in VB.NET. It may not suite your data source "as is" but works extremely well.

    https://1drv.ms/u/s!AtGAgKKpqdWjjG1UDQutKBOIxHTQ

    Thanks Karen, 

    Before I asked my question , i have tried your code 'CustomAutoCompleteTextBox' but it is out of my needs ... Now i have a simple code that achieve the mission well .. I think with some help we can get list appears

        


    Regards From Amr_Aly


    • Edited by Amr_Aly Wednesday, December 19, 2018 1:08 PM
    Wednesday, December 19, 2018 11:37 AM
  • Hi,

    try the way,

     Dim dtname As New DataTable
        Dim AllNames As DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim cn As New OleDbConnection(cs)
                cn.Open()
                Dim cmd As New OleDbCommand("select ProductName from Product", cn)
                Dim dadpter As New OleDbDataAdapter(cmd)
                Dim dset As New DataSet
                dadpter.Fill(dset, "Product")
    
    
            dtname = dset.Tables("Product")
    
    
        End Sub
    
    
        Private Sub cmbProductName_TextChanged(sender As Object, e As KeyPressEventArgs) Handles cmbProductName_TextChanged
            Dim rows As DataRow() = dtname.Select(String.Format("ProductName LIKE '%{0}%'", cmbProductName.Text))
    
            AllNames = dtname.Clone()
    
            For Each r As DataRow In rows
                AllNames.ImportRow(r)
            Next
    
        End Sub
    
        Private Sub cmbProductName_DropDown(sender As Object, e As EventArgs) Handles cmbProductName_DropDown
            cmbProductName.DataSource = AllNames.DefaultView
            cmbProductName.DisplayMember = "ProductName"
        End Sub
    End Class

    Best Regards,

    Alex

    Thanks Alex , 

    But it did not work ... I tried your suggestion , In your scenario list didn't appear .. But in my above code in (KeyDown event) list appears after I hit Enter Key  


    Regards From Amr_Aly


    • Edited by Amr_Aly Wednesday, December 19, 2018 4:11 PM
    Wednesday, December 19, 2018 4:10 PM
  • Hi,

    my code works well

    Imports System.Data.OleDb
    Imports System.Data.SqlClient
    Public Class Form1
    
        Dim AllNames As DataTable
        Dim dtname As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dtname.Columns.Add("ProductName")
            dtname.Rows.Add("alex")
            dtname.Rows.Add("peter")
            dtname.Rows.Add("james")
            dtname.Rows.Add("aaron")
            dtname.Rows.Add("TJ")
        End Sub
    
        Private Sub cmbProductName_TextChanged(sender As Object, e As EventArgs) Handles cmbProductName.TextChanged
            If dtname IsNot Nothing Then
                Dim rows As DataRow() = dtname.Select(String.Format("ProductName LIKE '%{0}%'", cmbProductName.Text))
    
                AllNames = dtname.Clone()
    
                For Each r As DataRow In rows
                    AllNames.ImportRow(r)
                Next
            End If
        End Sub
    
        Private Sub cmbProductName_DropDown(sender As Object, e As EventArgs) Handles cmbProductName.DropDown
            cmbProductName.DataSource = AllNames
            cmbProductName.DisplayMember = "ProductName"
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 20, 2018 7:40 AM
  • Hi,

    my code works well

    Imports System.Data.OleDb
    Imports System.Data.SqlClient
    Public Class Form1
    
        Dim AllNames As DataTable
        Dim dtname As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dtname.Columns.Add("ProductName")
            dtname.Rows.Add("alex")
            dtname.Rows.Add("peter")
            dtname.Rows.Add("james")
            dtname.Rows.Add("aaron")
            dtname.Rows.Add("TJ")
        End Sub
    
        Private Sub cmbProductName_TextChanged(sender As Object, e As EventArgs) Handles cmbProductName.TextChanged
            If dtname IsNot Nothing Then
                Dim rows As DataRow() = dtname.Select(String.Format("ProductName LIKE '%{0}%'", cmbProductName.Text))
    
                AllNames = dtname.Clone()
    
                For Each r As DataRow In rows
                    AllNames.ImportRow(r)
                Next
            End If
        End Sub
    
        Private Sub cmbProductName_DropDown(sender As Object, e As EventArgs) Handles cmbProductName.DropDown
            cmbProductName.DataSource = AllNames
            cmbProductName.DisplayMember = "ProductName"
        End Sub
    End Class

    Sorry Alex ,

    I didn't express well(your first code didn't give me the list with all names include the letter that i wrote in the combo box) .......  Now your code works like my code (list of combo box appears after hitting the drop down arrow), But i need the list appears when i write any letter in the combo box ..... Or i will return to my code .........

    Thanks Alex


    Regards From Amr_Aly

    Thursday, December 20, 2018 11:24 AM
  • Hi,

    I fix my code,it can meet your needs?

    Public Class Form1
        Dim AllNames As DataTable
        Dim dtname As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dtname.Columns.Add("ProductName")
            dtname.Rows.Add("alex")
            dtname.Rows.Add("peter")
            dtname.Rows.Add("james")
            dtname.Rows.Add("aaron")
            dtname.Rows.Add("TJ")
        End Sub
    
        Private Sub cmbProductName_TextChanged(sender As Object, e As EventArgs) Handles cmbProductName.TextChanged
            cmbProductName.Items.Clear()
            If dtname IsNot Nothing Then
                Dim rows As DataRow() = dtname.Select(String.Format("ProductName LIKE '%{0}%'", cmbProductName.Text))
    
                AllNames = dtname.Clone()
    
                For Each r As DataRow In rows
                    AllNames.ImportRow(r)
                Next
            End If
            For Each dtrow In AllNames.Rows
                cmbProductName.Items.Add(dtrow("ProductName"))
            Next
            Me.cmbProductName.Select(Me.cmbProductName.Text.Length, 0)
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 21, 2018 8:30 AM

  •  But i need the list appears when i write any letter in the combo box ..... Or i will return to my code .........

    Thanks Alex, 

    Looks good but it's equal to my original code(performs the same action [List appears after hitting the drop down arrow]) ... Now i think that if i want the list appears when writing i have to use Karen's example .... Really I tried it but i failed to make it works  for me and it's for text box not for combo box ....

    My code in the above and your recent code are now enough till i reach to my desired code 

    Thanks Alex .. I appreciated your time ....  


    Regards From Amr_Aly

    Friday, December 21, 2018 12:27 PM
  • Finally I got it ................

    It's so simple to make it work ..................

    This is for any one search for such a problem .........

    Add this line in cmbProductName_TextChanged 

    Me.cmbProductName.DroppedDown = True "

    That's it 

    Thank you Alex for your time 


    Regards From Amr_Aly

    • Marked as answer by Amr_Aly Saturday, December 22, 2018 8:07 PM
    Saturday, December 22, 2018 8:07 PM