HEllO guys I have been stuck for 3 weeks on this thing in vb .net. usually when we use suggestappend or autocomplete in a combobox for example three names in database retreived

1- Jack Nickelson
2- John Wether
3- Robert Garland

If I just remember the family name and I type "Nick" nothing will get retreived, I have to type Jack because autocomplete and suggestappend only retreive first letters of the two parted name. Please help to fix the code

That's how autocomplete works. It matches from the beginning. If you want to match on any string you can roll you own (after a fashion). Create a new form with a text box and a list box. Add the following code

Public Class Form1

    Private Movies() As String

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Movies = System.IO.File.ReadAllLines("d:\temp\movies.txt")
    End Sub

    Private Sub txtMovie_Enter(sender As System.Object, e As System.EventArgs) Handles txtMovie.Enter
        lbxMovies.Items.AddRange(Movies)
    End Sub

    Private Sub txtMovie_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtMovie.KeyUp
        lbxMovies.Items.Clear()
        lbxMovies.Items.AddRange(Filter(Movies, txtMovie.Text))
    End Sub

    Private Sub lbxMovies_Click(sender As System.Object, e As System.EventArgs) Handles lbxMovies.Click
        txtMovie.Text = lbxMovies.SelectedItem
    End Sub

End Class

For this example, put the following into a text file

All Good Things [2010].mp4
Being There [1979].mp4
Big Hero 6 [2014].mp4
Derailed [2005].mp4
Empire of the Sun [1987].avi
Fantastic Voyage [1966].mp4
Guardians of the Galaxy [2014].mp4
I Robot [2004].avi
Looper [2012].mp4
Man on a Ledge.mp4
Mr & Mrs Smith [2005].mp4
Mr Peabody & Sherman [2014].mp4
My Fair Lady [1964].mp4
Pacific Rim [2013].mp4
Pandorum [2009].mp4
Paul [2011].mp4
Penguins of Madagascar [2014].mp4
Secret of the Incas [1954].mp4
Silver Linings Playbook [2012].mp4
Snatch [2000].avi
Star Trek Into Darkness [2013].mp4
The Birds [1963].mp4
The Cabin in the Woods [2012].mp4
The Imitation Game [2014].mp4
The League of Extraordinary Gentlemen [2003].avi
The Road to El Dorado [2000].avi
The Secret Window [2004].mp4
The Theory of Everything [2014].mp4
Tucker and Dale vs Evil [2010].mp4
X-Men - Days of Future Past [2014].mp4

and change the code to read from that file. As you type in txtMovie, lbxMovies will change to reflect any movie title containing the typed string. Click on a movie in lbxMovies to select from the presented movies. If you wanted to get fancy you could auto-adjust the height of lbxMovies as the number of entries changes.

Thank you Jim but I am using combobox and sql database appyign the above into those two did not work..... Any help? I am getting when writing in combobox the second name :
1- Jack Nickelson
2- John Wether
3- Robert Garland
4- Sam Nickelson

when we type Nickelson I get Nickelson(2) without the first name near it to choose the right customer please check the code below:

Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Public Class Form3
    Dim cmd As New SqlDataAdapter
    Dim ds As New DataSet
    Dim searchstr As String
    Dim Wael As String





    Public Sub load_combobox1(ByVal cb As ComboBox, ByVal dSet As DataSet, ByVal id As String)
        Dim tRow As DataRow, tTbl As DataTable
        dSet.AcceptChanges()
        tTbl = dSet.Tables.Item(0)


        dSet.Dispose()


        ' fill out array by Company Names for cboRightCombo combobox
        cb.Text = ""
        cb.Items.Clear()
        cb.BeginUpdate()






        ' Load the Company Names into the ComboBox Control
        For Each tRow In tTbl.Rows

            cb.Items.Add(tRow(id).ToString)

            ' cb.Items.Add(tRow(val))
        Next
        cb.EndUpdate()

    End Sub


    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With ComboBox1
            .AutoCompleteSource = AutoCompleteSource.CustomSource
            .AutoCompleteMode = AutoCompleteMode.SuggestAppend '--\\
            ' .AutoCompleteSource = AutoCompleteSource.ListItems
            AddHandler .Validating, AddressOf cmb_Validating '// add ComboBox to Validating Event.

        End With

        'Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Me.CustomerTableAdapter.Fill(Me.GameDataSet1.Customer)


        Dim connectionstring As String = "Initial Catalog=Game;" & _
           "Data Source=user-TOSH;Integrated Security=SSPI;"
        Using Conn As New SqlConnection(connectionstring)

            Try
                Conn.Open()
                cmd = New SqlDataAdapter("select CustomerName from Customer", Conn)
                cmd.Fill(ds, "CUSTOMER")

                ComboBox1.DataSource = ds
                ComboBox1.DisplayMember = "CUSTOMER.CUSTOMERNAME"




                load_combobox1(ComboBox1, ds, "CUSTOMERNAME")
            Catch exc As Exception
                MsgBox(exc.Message)
                MessageBox.Show("error")
                Conn.Close()



            End Try
        End Using


        'make cursor on combobox
        'ComboBox1.Cursor = Cursors.Default




        ' ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        ' ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
        'ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownlist



        ' ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        'ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems


        '//-- set for AutoComplete.

        '// use this to load/reload the AutoCompleteList with the ComboBox items.
        loadMyCoolAutoCompleteList(ComboBox1)
    End Sub
    Private Sub loadMyCoolAutoCompleteList(ByVal selectedComboBox As ComboBox)
        With selectedComboBox
            .AutoCompleteCustomSource.Clear() '// Clear AutoCompleteList.

            For Each itm As String In selectedComboBox.Items '// loop thru all items in the ComboBox.


                If itm.Contains(" ") Then
                    .AutoCompleteCustomSource.Add(itm) '// add original item to your AutoCompleteList.
                    '// locate the .Substring you want to add to the AutoCompleteList.

                    itm = itm.Substring(itm.IndexOf(" ") + 1)
                    'Wael = itm.Substring(itm.TrimStart() + 0)  '// get all text following the ":" and the " " right after it.
                    .AutoCompleteCustomSource.Add(itm) '// add .Substring of item to your AutoCompleteList.

                End If
            Next

        End With
    End Sub
    '// once the ComboBox Validates (looses focus), it will set the original item as .Text.
    Private Sub cmb_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Dim selectedComboBox As ComboBox = CType(sender, ComboBox)
        For Each itm As String In selectedComboBox.Items '// loop thru all items in the ComboBox.

            If itm.Substring(itm.IndexOf(" ") + 1) = selectedComboBox.Text Then '// locate the .Substring that matches the .Text.
                selectedComboBox.Text = itm '// set .Text of ComboBox item.
            End If


        Next

    End Sub

    Private Sub combobox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged


    End Sub

End Class

Edited 1 Year Ago by wael_3

I may be confused about the question, but if the problem is getting the values from the database to the listbox you could do (I'm using a local sample database for the example)

Private Sub tbxName_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles tbxName.KeyUp

    Dim qry As String = "SELECT (au_fname + ' ' + au_lname) as aname FROM authors" & vbCrLf &
                        " WHERE (au_fname + ' ' + au_lname) LIKE '%" & tbxName.Text & "%'"

    Dim cmd As New SqlCommand(qry, con)
    Dim rdr As SqlDataReader = cmd.ExecuteReader

    lbxNames.Items.Clear()

    Do While rdr.Read
        lbxNames.Items.Add(rdr.GetString(0))
    Loop

    rdr.Close()

End Sub
This article has been dead for over six months. Start a new discussion instead.