Adding a “Tag” property to a ListBox's items, like a TreeView's Nodes?

后端 未结 2 507
-上瘾入骨i
-上瘾入骨i 2021-01-25 12:21

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 Win

相关标签:
2条回答
  • 2021-01-25 12:30

    You should create a custom SearchResult class 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.

    0 讨论(0)
  • 2021-01-25 12:50

    I would make a custom class that represents the items in the list. You have properties (not fields!) that represent the value to be displayed, and selected value you can enter that property name in the listbox's .DisplayMember and .ValueMember properties. Whatever is returned by DisplayMember will be displayed as the item's text, and whatever is returned by .ValueMember will be what .SelectedValue returns. You will be able to directly access .SelectedItem to get the exact item that was selected. This way you can store anything you want as the items. If you don't specify a .DisplayMember then the listbox will use the .ToString function on the item.

    0 讨论(0)
提交回复
热议问题