ListBox String

tonycrew

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

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

What im trying to do here is it Selects the first item in listbox4 (did the in previous code), And then if the item is like 'Sonic The Hedgehog 1 (USA)' and in listbox5 there is an item 'Sonic The Hedgehog (UK)' then it will select that listbox5 item (also known as finding a string almost exact)..

But there is an error saying..

VB.NET:
Object reference not set to an instance of an object.

any help?
Thanks!
 
The first thing to note is that FindString returns an Integer, so why are you assigning its result to a String? It returns the index of the item it found, or -1 if no item was found.

FindString is going to find the first item where the text starts with the value you specify. "Sonic The Hedgehog (UK)" does not start with "Sonic The Hedgehog 1 (USA)" so FindString will return -1.

You're then trying to find the index of the item "-1" in ListBox5, which is -1, i.e. no such item exists. You then set the SelectedIndex of ListBox5 to -1, so no item is selected. You then try to use the SelectedItem, which is Nothing, and you get your exception.

The ListBox is not going to perform a fuzzy search for you. If you want to seach for an item that starts with the same substring as some other value starts with then you're going to have to implement the logic yourself.
 
Back
Top