I’m making a Windows Form project that will search for files in the specified folders in my spare time. So far, I’ve got my TreeView that displays folders just like in the Windows Explorer.
I also have a ListBox that will store all the search results. When I double click an item in that list, I want to have the option of opening the file.
So far, I see three ways of doing this:
- Having the ListBox display the full path.
- Using a global List or Array to keep track of the full paths, while the ListBox displays only the file name. Then use the index to match the corresponding items.
- Adding a “Tag” property to each of my ListBox’s items, just like a TreeView’s nodes.
To me, the third method is the most appealing, but I have no idea where to start. Up until now, I’ve been fiddling with a Control Library to no avail.
I was hoping to be able to get the Tag to work in a similar fashion as with the TreeView. So, the code to retrieve the tag and set the tag would look like ListBox1.Items[i].Tag or maybe ListBox1.Tag[i] if the former is not possible.
Any help would be greatly appreciated.
You should create a custom
SearchResultclass with a property that returns the full path.The class should override
ToString()and return the text you want to display in the listbox.You can then put instances of your class directly into the listbox, and cast an item from the listbox back to the class to get the property.