Click here to Skip to main content
15,072,046 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list which contains an amount of words. I also have a textbox. When the user presses a button, the program should see if a word inside the text of the textbox (Example: "This is my text.") is contained inside my listbox. I can't use this:
VB
If listbox4.items.contains(textbox1.text) = true then
'My code
End If


All ^^^that code does is check if ALL of the text in the textbox matches an item in the listbox. I only want it to check if a certain word in a sentence of the text in my textbox is found in my listbox.

It's like a never ending loop! << according to my tired thursday brain

Eg. (silly code of madness):
VB
If textbox1.text.contains(listbox4.items.collection.tostring or an item in my listbox) then
'My code
End If


Any help would be appreciated! If there's a certain "thing" or method of some sort I should know of, please let me know!

BTW: I have searched the internetz for help!
Posted
Updated 7-May-14 23:47pm
v2

First, you have to split the string in the textbox into array of words delimited by space. Next loop thru the array to check if any of the word element is contained in the listbox.

VB
Public Class Form9

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        ' Split string based on space
        Dim textsrtring As String = TextBox1.Text
        Dim words As String() = textsrtring.Split(New Char() {" "c})
        Dim found As Boolean = False

        ' Use For Each loop over words
        Dim word As String
        For Each word In words
            If ListBox1.Items.Contains(word) Then
                found = True
                Exit For
            End If
        Next

        MessageBox.Show(found)

    End Sub

    Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("AAAA")
        ListBox1.Items.Add("BBBB")
        ListBox1.Items.Add("CCCC")
        ListBox1.Items.Add("DDDD")
    End Sub
End Class
   
v3
Comments
Nathan Lecompte 8-May-14 6:45am
   
Ok this works! Thanks, much appreciated!

PS: If you're wondering what I'm doing, I'm making something called Lumia Browser: http://lumiatechnologies.com/projects
It is actually simple: split the text of your textbox using the space as separator and search for each of the word the result array contains. You could stop at firsh match or retrieve all the list items matching any of the word.
   
Comments
Nathan Lecompte 8-May-14 6:46am
   
Thanks for your solution too!
CPallini 8-May-14 11:22am
   
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900