生成了文件名列表,但没有文件路径 [英] Generated a list of filenames, but no file paths

查看:41
本文介绍了生成了文件名列表,但没有文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,显示一组引用文本文件的文件名.我认为显示完整路径在美学上没有吸引力,所以我使用 Path.GetFileName 来切断目录部分.

I have a list box that displays a set of filenames that reference text files. I think it is aesthetically unappealing to display full paths, so I used Path.GetFileName to cut off the directory part.

但是现在当用户选择要打开的特定文件名时,我丢失了路径.这些文件可以位于本地计算机上的任何位置(目前).

But now when the user selects a particular filename to open, I've lost the paths. The files could be located anywhere on the local computer (for now).

如何使用列表框来显示漂亮的文件名,同时还可以引用实际文件?

How can I use the list box so that I can display nice filenames, but also have reference to the actual file?

我喜欢为每个列表框项目有一个自定义包装类的想法.

I like the idea of having a custom wrapper class for each list box item.

推荐答案

我过去所做的是为我想在 ListBox 中显示的对象创建一个包装类.在此类中,将 ToString 覆盖为要在 ListBox 中显示的字符串.

What's I've done in the past is create a wrapper class for the objects I want to display in the ListBox. In this class override ToString to the string you want to display in the ListBox.

当您需要获取所选项目的详细信息时,将其转换为包装类并提取您需要的数据.

When you need to get details of a selected item, cast it to the wrapper class and pull the data you need.

这是一个丑陋的例子:

class FileListBoxItem
{
    public string FileFullname { get; set; }
    public override string ToString() {
        return Path.GetFileName(FileFullname);
    }
}

用 FileListBoxItems 填充您的列表框:

Fill your ListBox with FileListBoxItems:

listBox1.Items.Add(new FileListBoxItem { FileFullname = @"c:\TestFolder\file1.txt" })

像这样取回所选文件的全名:

Get back the full name of a selected file like this:

var fileFullname = ((FileListBoxItem)listBox1.SelectedItem).FileFullname;

<小时>

编辑
@user1154664 在对您原始问题的评论中提出了一个很好的观点:如果显示的文件名相同,用户将如何区分两个 ListBox 项目?


Edit
@user1154664 raises a good point in a comment to your original question: how would a user differentiate two ListBox items if the displayed file names are the same?

这里有两个选项:

同时显示每个 FileListBoxItem 的父目录

要做到这一点,请将 ToString 覆盖更改为:

To do this change the ToString override to this:

public override string ToString() {
    var di = new DirectoryInfo(FileFullname);
    return string.Format(@"...\{0}\{1}", di.Parent.Name, di.Name);
} 

在工具提示中显示 FileListBoxItem 的完整路径

为此,在您的表单上放置一个 ToolTip 组件,并为您的 ListBox 添加一个 MouseMove 事件处理程序以检索 FileLIstBoxItemFileFullname 属性值代码> 用户将鼠标悬停在上面.

To do this drop a ToolTip component on your form and add a MouseMove event handler for your ListBox to retrieve the FileFullname property value of the FileLIstBoxItem the user is hovering the mouse over.

private void listBox1_MouseMove(object sender, MouseEventArgs e) {
    string caption = "";

    int index = listBox1.IndexFromPoint(e.Location);
    if ((index >= 0) && (index < listBox1.Items.Count)) {
        caption = ((FileListBoxItem)listBox1.Items[index]).FileFullname;
    }
    toolTip1.SetToolTip(listBox1, caption);
}

当然,您可以将第二个选项与第一个选项一起使用.

Of course you can use this second option with the first.

来源 用于列表框中的工具提示(已接受的答案,将代码重新格式化为我喜欢的风格).

Source for the ToolTip in a ListBox (the accepted answer, code reformatted to a flavor I prefer).

这篇关于生成了文件名列表,但没有文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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