如何循环遍历 ListView 的特定子项?
我正在制作一个程序,它使用 ListView 来查看所选目录中的目录和文件。 ListView 本身有两列,并设置为详细信息视图。两列是文件夹名称(通过按钮选择的目录)和电影名称(将在 ListView 上列出所选目录中的所有电影)。
我希望 ListView 做的是在将文件夹添加到 ListView 后,子项目应该隐藏,直到单击左侧列(选定的目录)。
我试图循环遍历 ListView 的子项,但问题是它不仅打印出目录中的所有文件和文件夹,而且还循环遍历并打印出目录名称。
对于那些想查看代码和项目的人,我在下面提供了下载链接。
http://www8.zippyshare.com/v/75161785/file.html
这里也是我的代码的一部分,用于显示目录中的文件和文件夹数组,正如您所看到的,我的问题是它也循环遍历我的项目,我希望它只显示一次。
for (int i = 0; i < jointArray.Length; i++)
{
ListViewItem item = new ListViewItem(fi.Name);
item.SubItems.Add(jointArray[i]);
listView1.Items.Add(item);
}
I'm making a program that uses a ListView to view the directories and files in the selected directory. The ListView itself has two columns and is set to Details view. The two columns are Folder Name (the directory selected from a button) and Movie Names (which will list all movies in the selected directory on the ListView).
What I would like the ListView to do is after the folder has been added to the ListView the subitems should be hidden until the left-hand column (selected directory) is clicked.
I have attempted to loop through the subitems of a ListView, but the problem is that not only does it print out all the files and folders within the directory, it also loops through and prints out the directory name as well.
For those that would like to see the code and project I have provided a Download link below.
http://www8.zippyshare.com/v/75161785/file.html
Also here is part of my code to display the array of files and folders within a directory, as you can see my problem is that it also loops through my item which i would like it to display only once.
for (int i = 0; i < jointArray.Length; i++)
{
ListViewItem item = new ListViewItem(fi.Name);
item.SubItems.Add(jointArray[i]);
listView1.Items.Add(item);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您需要一个 TreeControl,每个目录都会折叠起来,直到您单击它为止(很像 Windows 资源管理器)。
然后您可以递归地查看所有文件夹和文件夹。文件并将它们添加到树视图中。
Looks like you need a TreeControl with each directory collapsed until you click it (much like windows explorer).
Then you can recursively look through all your folders & files and add them to the treeview.