ListBox find string

tonycrew

Well-known member
Joined
Apr 20, 2009
Messages
55
Programming Experience
Beginner
Hello i have this..

VB.NET:
                    ListBox4.SelectedIndex = 0
                    Dim mrc As Integer = ListBox5.FindString(ListBox5.Items.IndexOf(ListBox4.SelectedItem))
                    ListBox5.SelectedIndex = mrc
                    Dim lb4 As Integer = ListBox4.Items.IndexOf(ListBox4.SelectedItem)
                    TreeView1.Nodes(lb4).Nodes.Add(ListBox5.SelectedItem.ToString)

What im trying to do is, Make ListBox5 select the index of ListBox4 Selected Item (The Matching Ones).. example: I have the files of the same in two listboxes, Then in ListBox4, it selects The first Item (Got that bit), Then it selects the item of the exact same string as listbox4 selected item to select in listbox5..

Please help, Thanks :)
 
Last edited:
Is it just me or does the description of what you're trying to do sound confusing?

It's probably just me cause it's Sunday and my brain doesn't kick into gear again till tomorrow.
 
What it's trying to do is 2 listbox's one has one lot of filenames in and so does the other. if they match they will be selected.

What has happened before this point is that 2 directories were selected with files in.
It then lists each dir in a listbox, we will then compare filenames for a similar match.

i.e. listbox4 conatins sonic the hedgehog 1 (usa).zip
and listbox5 contains Sonic The Hedgehog (EU).png

Then you can choose to rename listbox5 to match. Through node selections. But it should automatically put each file in the correct node/child node.
 
Use a pair of nested For-Next loops to compare each string with the other. Use either ToUpper or ToLower to find a match without regard to case. If a match is found, then tag it to be selected.

Pseudocode:

For Each itemA in Listbox1
For Each itemB in Listbox2
If itemA.ToUpper() = itemB.ToUpper() Then
select them
End If
Next itemB
Next itemA
 
Last edited:
i think it might work mate.. but i tried,

VB.NET:
                    For Each itemA In ListBox4.Items
                        For Each itemB In ListBox5.Items
                            If itemA.ToUpper() = itemB.ToUpper() Then
                                ListBox5.SelectedItem = itemB
                            End If
                        Next itemB
                    Next itemA

is that right..

and i get an error reguarding

VB.NET:
Next itemB

saying..

VB.NET:
List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.
 
Don't change the lists until after the loops have ended. Just save the matches to a new array and then use the array to make the changes to the lists afterwards.
 
Back
Top