2012-08-08 56 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); 
} 

只是這裏的想法是主要的東西

相關問題