2012-08-08 57 views
1

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

但现在当用户选择一个特定的文件名打开时,我已经失去了路径。这些文件可以位于本地计算机上的任何地方(现在)。

如何使用列表框,以便我可以显示漂亮的文件名,但也有实际的文件参考?

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

+0

如果有两个文件具有相同的FileName,但在不同的路径会发生什么?用户如何知道选择哪一个? – user1154664 2012-08-08 22:24:24

+0

横渡你的手指,并希望最好的。我将提供一个显示完整路径的选项。 – MxyL 2012-08-08 22:25:59

回答

2

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

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

这里是一个丑陋的例子:

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

填写您的列表框与FileListBoxItems:

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

找回这样的所选文件的全名:

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

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

这里有两种选择:

还显示每个FileListBoxItem的父目录

要做到这一点的ToString覆盖改成这样:

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

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

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

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); 
} 

当然,您可以在第一个选项中使用第二个选项。

Source为列表框中的工具提示(接受的答案,代码重新格式化为我喜欢的味道)。

1

如果使用WPF,则使用ListBoxItem.Tag来存储每个项目的完整路径。或者,如果使用WinForms,则可以创建一个存储完整路径的自定义类,但会覆盖object.ToString(),以便仅显示文件名。

class MyPathItem 
{ 
    public string Path { get; set; } 
    public override string ToString() 
    { 
     return System.IO.Path.GetFileName(Path); 
    } 
} 

... 

foreach (var fullPath in GetFullPaths()) 
{ 
    myListBox.Add(new MyPathItem { Path = fullPath }); 
} 
+2

我讨厌人们开始使用Tag属性作为他们个人的垃圾桶。我看到代码由于缺乏对该属性的纪律而导致很多时间失败,这是从没有其他选择的日子遗留下来的老式做法。 – slugster 2012-08-08 23:39:04

1

就我个人而言,我不同意你的观点,这对用户来说是丑陋的。显示完整的路径给用户提供了明确的细节,并使他们对他们的选择或他们正在做的事情有信心。

我会使用Dictionary,使用项索引作为键和此列表项的完整路径作为值。

Dictionary<int, string> pathDict = new Dictionary<int, string>(); 
pathDict.Add(0, "C:\SomePath\SomeFileName.txt"); 
... 

以上的可能是要走在这里使用item.Tag财产的最好方式......

我希望这有助于。

+0

我想我总是可以提供一个选项来显示或隐藏目录路径,以防用户喜欢它们。 – MxyL 2012-08-08 22:17:01

1

我这样做

public class ListOption 
{ 
    public ListOption(string text, string value) 
    { 
     Value = value; 
     Text = text; 
    } 

    public string Value { get; set; } 
    public string Text { get; set; } 
} 

然后创建我的列表

List<ListOption> options = new List<ListOption>() 
For each item in files 
    options.Add(new ListOption(item.Name, item.Value)); 
Next 

绑定我的名单

myListBox.ItemSource = options; 

然后让我的值或文本

protected void List_SelectionChanged(...) 
{ 
    ListOption option = (ListOption) myListBox.SelectedItem; 
    doSomethingWith(option.Value); 
} 

只是这里的想法是主要的东西

相关问题