Hello Programmers,

Your newbie programmer wants to ask a help regarding his program (That’s Me :D). My program is in Visual Basic 2010, I want to ask how I can search a words or strings. I use TextBox, ListBox and Button for my program. For detailed explanation about what my problem is, I provide image below:

VB1new

Above image is my program design, as you can see there’s a Button(Search), TextBox(Search Here) and ListBox(w/a words of “Array’, “Bytecode”, “Compile”, “Computer”, “Debug”, Etc…) All I want to learn is how to rearranged/filter the words in ListBox when I typed a word in TextBox(Search Here) and click the Button(Search). For example, I type the word “Compile” in TextBox(Search Here), when I click the Button(Search), the word in the ListBox “Compile” must be in top instead of word “array” and “bytecode” and next word must be “Compiler” as it is same near in the word “Compile”.
The codes I use in my Button(Search) is this:

    Dim count As Integer = (ListBox1.Items.Count - 1)

    Dim words, wordsB  As String

    Dim spaceCharCounter As Integer = 0

    For i = 0 To count

        words = ListBox1.Items.Item(i)

        For j = 0 To words.Length - 1

            If spaceCharCounter >= 2 Then

                wordsB += words.Substring(j, 1)

            ElseIf words.Substring(j, 1) = Chr(32) Then

                spaceCharCounter += 1

            End If

        Next

        If InStr(wordsB, TextBox1.Text) Then

            ListBox1.Items.Add(ListBox1.Items.Item(i))

        End If

        words = Nothing

        spaceCharCounter = 0

        wordsB = Nothing

    Next

My codes above will just highlight the items in the ListBox that is matched to the word I search.

Please help me guys... As you can see in my program design I try to create a simple dictionary of programming. Comment your Comments, Suggestions and Any Idea that can help regarding this program :D
Thanks in Advance :D

Hi, since the items in your listbox are in alphabetical order already, all you need to do is shift the positions of them. This can be done with a loop that checks for the word in textbox. The code attached does that and than puts it and the items after it in the string words. Then it starts at the top of the listbox again until the word in textbox is found a second time and ends the loop. After that the listbox is cleared and populated with the items in the string words. Remark: compairing the word in the textbox with the ones in your listbox is case sensitive. So "compile" instead of "Compile" won't give a result. Good luck with this.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim words As String = ""
        Dim check As Integer = 0
        Do While check < 2
            For Each item In ListBox1.Items
                If TextBox1.Text = item Then
                    check = check + 1
                    If check = 2 Then
                        Exit Do 'ends the search
                    End If
                    words = words + item
                End If
                If check = 1 And TextBox1.Text <> item Then
                    words = words & vbCrLf & item
                End If
            Next
        Loop
        ListBox1.Items.Clear()
        Dim lines() As String = Split(words, vbCrLf)
        For Each line As String In lines
            ListBox1.Items.Add(line)
        Next
    End Sub
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.