I'm trying to create a program, where a user can use the textbox to search for information, and as soon the user begins to type, any matching results will show autofill. I was able to do that, but I also want to make partial word match: words are found that only match partially (Example search: "apple" also finds "pineapple"). Could some one help me in achieveing this?

This is what I have so far:

Public Class Form1
    'Declaration
    Public Property AutoCompleteMode As AutoCompleteMode
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Create the list to use as the custom source. 
        Dim MySource As New AutoCompleteStringCollection()
        MySource.AddRange(New String() _
                            { _
                                "January", "Joke", "James", "Joy", _
                                "February", _
                                "March", _
                                "April", _
                                "May", _
                                "June", _
                                "July", _
                                "August", _
                                "September", _
                                "October", _
                                "November", _
                                "December" _
                            })

        ' Initialize the text box. 
        TextBox1.AutoCompleteCustomSource = MySource
        TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource

    End Sub

End Class

I think you have to use the StartWith() or EndWith methode so that it can search for all the words that StartWith(textbox1.text) Or EndWith(TextBox1.text) I'm sure you can achieve your goal by that.

Yeah, but what if the word matches in between? not the beginning nor the end.

Well in that case I'm not sure but I think you will have to first set instructions to first search the character by character that the user enter when ever the key is pressed so that you will try to match the characters while the user is typing in, I'm sure you will have to use either the timer nor textbox change event handle that refer it to you library where you have the collection of your words that you want to match from.

I've just used the .Contains() method I think in order for you too to match the in between you have to use the Contain() method and inside the () you have to enter the library of your collections of your match list to match it up. Hope this will answer your question this keep track of your text box till the word in contain is entered it will then start to fire the correct commands.

Mr.M could you give me an example of using the .Contains() method.

Here is a very simple example on how to use the Contains method.

If TextBox1.Text.Contains("my word") Then ' Here you can replace the my word with the library of your words where you have the collections of words that you want to see the word if it match.
MsgBox("The item is found")
Else
MsgBox("No item found")
End If

Now to test this create a new project and add a text box and a button and type the above codes in a button click event the debug it and key any word you like in a text box and click a button you will see that the item is not found now type the word my word in a textbox and click the button now you will get a message that the item is found so that how you go about matching the contains from a tool

OK Mr.M, but what if I need to be able to search with Partial Word Match. For example having first and last name give out the result by any matching letters within the collection.

Having names such as: Lila Harpz, Matt Ilamin
and searching in the textbow by entering 'ila',
will display both Lila Harpz and Matt Ilamin.

And is it possible to display a list of matching cases as the user inputs his search? Just like you see autofill search when searching Google.

Well I haven't done this but I think to get the list of Containing words I think you can loop it like use the For Each contain word and also if you are using a listbox or textbox you can set it to multiple lines so that if the word you want shows up you can click it. also for auto show the matching words while the user type I think you can trick it by using a timer that means you will put your codes in timer or use the Textbox KeyChange event handler so as the user types each alphabet it will display the matching words remember to use the For Each Loop to get the list or collection of matching words.

You have to modify it as you need it. I put a second textbox and sat it to multiline

Public Class Form1
    'Declaration
    Public Property AutoCompleteMode As AutoCompleteMode
    Public MySource As New AutoCompleteStringCollection()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Create the list to use as the custom source. 

        MySource.AddRange(New String() _
                            { _
                                "January", "Joke", "James", "Joy", _
                                "February", _
                                "March", _
                                "April", _
                                "May", _
                                "June", _
                                "July", _
                                "August", _
                                "September", _
                                "October", _
                                "November", _
                                "December" _
                            })
        ' Initialize the text box. 
        TextBox1.AutoCompleteCustomSource = MySource
        'TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource

    End Sub

    Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
        Dim strOr As String, strReturn As String
        strOr = Trim(TextBox1.Text)
        TextBox2.Text = ""
        For Each value As String In MySource
            'strLastName = Mid( StrFullName, Instr(strFullName, " "))
            If InStr(value, strOr) > 0 And strOr <> "" Then
                'strReturn = Mid(value, InStr(value, strOr))
                strReturn = value
                Debug.Print(strReturn)
                TextBox2.Text = TextBox2.Text & strReturn & vbNewLine
            End If

        Next
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class

Thank you for the code Minimalist. I've chaged the TextBox2 to ListBox1, and added two cllections of items (for testing purposes) in the My Project --> Settings. I've made the collections to be displayed correctly in according with the checked radio buttons. Now when I search in the textbox, I get the matched case of words higlighted in the listbox, but on top of that how would I make the non-matching items dissapear when making a search, and appear back if the user erases their search?

Where do you want the words disappear? Listbox? Textbox?

The Listbox, where the user can see the collection of items. This way, only the matching case items will remain displayed for the user.

O.K. take it step by step: after you highlight all the matching words in your program you delete the not highlighted words. The following code is just highly guessing.

Dim I As Integer
    For I = Me.ListBox1.ListCount - 1 To 0 Step -1
        If Not(Me.ListBox1.Selected(I)) Then
            Me.ListBox1.RemoveItem (I)
        End If
    Next

I think I just thought of better waydoing what you want. May be before each search clear the listbox and than do the new search so you only have the newest st of wor matches in the listbox.

I think I just thought of better waydoing what you want. May be before each search clear the listbox and than do the new search so you only have the newest st of wor matches in the listbox.

That could be a better idea, but I want the users to be able to see the list of all the containing items before I get to remove them as they begin searching.

I also tried to plug in the the code you provided and tried editing, but line 3, Gives error of "'Selected' is not a member of 'System.Windows.Forms.ListBox'." and I'm not sure how to resolve this. Any suggestions?

O.K. as I said the first attempt was just a guess and a hangover from vb6.
ListBox1.ListCount becomes listbox.items.count, however we need to use the a list to add all the selected items, move these into an array, clear the listbox and add the selected items back into the listbox. I put the code to do this in a button I added to the form

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim theLsect As String
        theLsect = Nothing

        Dim itemList As New List(Of String)
        For Each s As String In ListBox1.SelectedItems
            itemList.Add(s)
        Next
        Dim itemArr() As String = itemList.ToArray
        Dim SectionArray() As String = itemArr
        ListBox1.Items.Clear()
        For Each s As String In itemArr
            ListBox1.Items.Add(s)
        Next
    End Sub

Minimalist, this indeed helps me, however its only part of what I need. This code removes all the items for the exception of the one which is highlighted, but I need for all items searched and matched with the case to remain in the listbox after performing a search in the textbox.

Example of what I need:

Before search is performed

Textbox search: ""
Listbox items: Bay, Charles
               Hose, David
               Remaldin, Jade
               Harpert, James
               Termol, Karol
               Protom, Case
               Marinal, Ace

After search is performed

Textbox search: "c"
Listbox items: Bay, Charles
               Protom, Case
               Marinal, Ace

As it can be seen, all items which have the character 'c', remain in the listbox while all non-matching items are being removed.

You need to set your listbox in the properties window to multiselect and and select each item with a match that matches.

OK, I've set the listbox SelectionMode to MultiExtended, but I'm not sure how to

select each item with a match that matches.

Because when I searched the collection, the items get highlighted in a wierd way. For example I searched for "se", and this highlights the names "Bay, Charles" and "Hose, David", but "Protom, Case" is being left out! Even though it has the "se" characters in the name. The "Bay, Charles" gets highlighted when I don't want it to be, because the exact order of "se" was not in the name. I also wonder wheather it's possible to delete the non-matching names with the search of at least one character, not two, like I have to right now. So what I mean by that is when the user beggins typing "b" for example, the name "Termol, Karol" will immediately disappear because it doesn't have a "b" anywhere within that name.

I have used as selection mode "multi simple" - maybe try this and let me know

The same problem persists after using "multi simple" as selection mode . Also, when I begin typing 'r', only "Bay, Charles" gets highlighted, after I add 'o', so now I'm searching for 'ro', "Bay, Charles" still remains highlighted and "Termol, Karol" also gets highlighted. However "Bay, Charles" shouldn't remain highlighted after performing a search for 'ro', since there is no 'o' character. This is beyond weirdness. And when I backspace the 'o', the name "Termol, Karol" remains highlighted, which by logic shouldn't. This is a little frustrating for me. Got lost on how to fix this issue.

O.K. I got it working, however it distinguishes between captal and non capital letters. I put 2 buttons, one to load your list into the listbox and one to remove non highlihted words - which gives the same results as in the multiline textbox.
Here is the whole code:

Imports System.IO
Public Class Form1
    'Declaration
    Public Property AutoCompleteMode As AutoCompleteMode
    Public MySource As New AutoCompleteStringCollection()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MySource.AddRange(New String() _
                            { _
                                "January", "Joke", "James", "Joy", _
                                "February", _
                                "March", _
                                "April", _
                                "May", _
                                "June", _
                                "July", _
                                "August", _
                                "September", _
                                "October", _
                                "November", _
                                "December" _
                            })
        ' Initialize the text box. 
        TextBox1.AutoCompleteCustomSource = MySource
        'TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource

    End Sub

    Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
        Dim strOr As String, strReturn As String
        strOr = Trim(TextBox1.Text)
        TextBox2.Text = ""
        ListBox1.ClearSelected()
        For Each value As String In MySource
            'strLastName = Mid( StrFullName, Instr(strFullName, " "))
            If InStr(value, strOr) > 0 And strOr <> "" Then
                'strReturn = Mid(value, InStr(value, strOr))
                strReturn = value
                TextBox2.Text = TextBox2.Text & strReturn & vbNewLine
                ' put code for highlighting here
                Dim ind As Integer = ListBox1.FindString(strReturn)
                If ind <> -1 Then
                    ListBox1.SetSelected(ind, True)
                End If
            End If
        Next
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim theLsect As String
        theLsect = Nothing
        Dim itemList As New List(Of String)
        For Each s As String In ListBox1.SelectedItems
            itemList.Add(s)
        Next
        Dim itemArr() As String = itemList.ToArray
        Dim SectionArray() As String = itemArr
        ListBox1.Items.Clear()
        For Each s As String In itemArr
            ListBox1.Items.Add(s)
        Next
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim str As String
        For Each str In MySource
            ListBox1.Items.Add(str)
        Next
    End Sub
End Class

I think I added only 4 lines to it. Just see if you underszand it aand let me know

Thats what the picture looks like sfter I pt an o into the input textbox:

I was wondering if it's possible to make it not case sensitive? And also instead of having the second multiline textbox where only the matching items are shown, have that work within the listbox? Because I need this to work in the listbox, since those items which remain in the list after the search, can be clicked, for I'm creating a database with a collection of items. I really appreciate your help.

Well you would have to put some code in to make it not case sensetive - I need to think about this. To the second point, the bclear button clears all the non selectred items in the list box and I can compare in the developing state that I get exactly the same words that are highlighted. You can remove this and vou can put the code for removing non selected items somewhre else. But why did you wanted all your words in the listbox? You could have moved only the matching words as in the textbox and you wouldn't need to highlight and than remove. I am not clear about this.

The reason why I want this, is to first let my user see the options from which they can choose, instead of having to guess and search for every letter a-z until they find what they need. So they basically have two options that way: one is to look through the listbox untill they find what they need, or two: have the list there, but instead of scrolling through the long list, use the textbox to search for the items within the listbox.

The problem is that you can get a combination of lower and upper case letters in the owrd list and mybe typos. So what you can try is to declare another string variable to change all inpu to lower case, do the same for the string that is returned from your list, compare these and if a match is found highlight the original word in your list.

Wow. That's advanced, too advanced for me.

It only takes a few lines and works a charm. Icommented the code a bit. I removed the button to populate the list and do it at form load. The codeposted goes into the TextBox1_KeyUp event.

 Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
        Dim strOr As String, strReturn As String, strL As String, valueL As String
        strOr = Trim(TextBox1.Text)
        strL = UCase(Trim(TextBox1.Text)) 'hold input in lower case
        TextBox2.Text = ""
        ListBox1.ClearSelected()
        For Each value As String In MySource 'returns the origanal string from list
            valueL = UCase(Trim(value)) 'hold string from list in lower case
            If InStr(valueL, strL) > 0 And strOr <> "" Then 'now compare only lower cases
                strReturn = value ' now we found a match so we use the original string again
                TextBox2.Text = TextBox2.Text & strReturn & vbNewLine
                ' put code for highlighting here
                Dim ind As Integer = ListBox1.FindString(strReturn)
                If ind <> -1 Then
                    ListBox1.SetSelected(ind, True)
                End If
            End If

        Next
        If TextBox1.Text = "" Then
            ListBox1.Items.Clear()
            Dim str As String
            For Each str In MySource
                ListBox1.Items.Add(str)
            Next
        End If

Good luck and if that is all please marke the thread as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.