Help With Looping Through Listbox

Azreal

Member
Joined
Oct 4, 2006
Messages
19
Programming Experience
Beginner
Hello, how would I push a button on my form and have that see if a listbox contains a word(s) in a textbox?

Say the listbox has the words:
"hello", "lake", "cat", "pants", or anything in it.

And say you have the following in the textbox
"Hello, my name is Azreal. I live in a small house by the lake."

The textbox contains a word(s) that is in the listbox, so a msgbox pops up saying so.

How would I do this?

Thanks​
 
Hopefully the logic alone will help you. Give the chance to learn something from the exercise.

Setup an arraylist to hold all the items in the listbox.
Loop through the listbox, adding each item to the array list.

Setup a second array list to hold the words in the text box.
Split the Textbox.Text value based on " " (space) and any punctuation you need to consider.

Loop For Each Item In ArrayListOfListBoxItems
Loop For Each Item In ArrayListOfTextBoxWords
if match, display message
Next (Next element from textBox arraylist)
Next (Next element from listbox arraylist)
 
it's a collection that acts closely to an array, arraylists are IMO the easiest collection to use, they're even easier than arrays are IMO
 
Also as you are using 2005 i would suggest using a generic list instead of an arraylist. So rather than

VB.NET:
Dim MyArrayList as new ArrayList

Instead use..

VB.NET:
Dim MyList As New List(Of T)

The generic collections are up to as much as 7 times faster than the standard arraylist.
 
Would it be easier to do what has been said, but instead of cloning the listbox items into a arraylist, maybe just compare the textbox words arraylist against the listbox directly?

Or am i being an idiot? (somtimes my methods are not the most efficent! :) )
 
Would it be easier to do what has been said, but instead of cloning the listbox items into a arraylist, maybe just compare the textbox words arraylist against the listbox directly?

Or am i being an idiot? (somtimes my methods are not the most efficent! :) )

No, that would work just fine. I over-complicated it a bit, good catch ;-)
 
Back
Top