添加“标签”。属性的列表框的项目,如TreeView的节点? [英] Adding a "Tag" property to a ListBox's items, like a TreeView's Nodes?

查看:76
本文介绍了添加“标签”。属性的列表框的项目,如TreeView的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Windows Form项目,该项目将在业余时间搜索指定文件夹中的文件。到目前为止,我的TreeView就像Windows资源管理器一样显示文件夹。

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.

我还有一个ListBox,它将存储所有搜索结果。当我双击该列表中的一个项目时,我想选择打开文件。

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:


  1. ListBox显示完整路径。

  2. 使用全局List或Array跟踪完整路径,而ListBox仅显示文件名。然后使用索引来匹配相应的项。

  3. 向每个ListBox项添加一个 Tag属性,就像TreeView的节点一样。

对我来说,第三种方法最吸引人,但我不知道从哪里开始。到目前为止,我一直在摆弄控制库,但无济于事。

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.

我希望能够使Tag以与以下类似的方式工作TreeView。因此,用于检索标签和设置标签的代码应类似于 ListBox1.Items [i] .Tag ListBox1.Tag [i]

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.

任何帮助将不胜感激。

推荐答案

您应创建一个自定义的 SearchResult 类,该类应具有返回完整路径的属性。

该类应覆盖 ToString()并返回要在列表框中显示的文本。

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.

这篇关于添加“标签”。属性的列表框的项目,如TreeView的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆