2016-03-11 18 views
1

當啓用multiselect的情況下使用OpenFileDialog時,每次我選擇其他文件時(使用Ctrl或Shift +單擊),最近添加的文件都會插入到文件名文本框的開頭。有沒有辦法改變這一點,並使它們添加到最後呢?可以在最後添加選定的文件,而不是開始使用OpenFileDialog和多選?

我正在做一些與IFileDialog接口的工作來定製它,文件排序對我來說至關重要。

我正在使用.NET 4.5。

編輯:在做了一些更多的實驗之後,我不確定文件在返回後的排序情況。它似乎是按字母順序的。任何人都可以驗證此?我無法爲此找到很好的文檔/示例。

+1

那麼反轉Filenames數組呢? – Steve

+0

我實際上試圖改變它們在對話框仍然打開時顯示的順序(在用戶選擇OK之前)。在我的問題中,我可能沒有說清楚。 –

+1

不,它很明顯,但不清楚(並觸發了我的答案)是原因。我認爲重要的是你在選擇之後執行的代碼,而不是它們如何顯示。在任何情況下,我認爲他們選擇以這種方式添加它們以便於在添加許多文件時看到最後一個文件,並且其中一些文件從文本框滾動。 – Steve

回答

3

如果您想按照您點擊它們的確切順序來獲取所選文件,則無法使用標準OpenFileDialog,因爲您無法控制返回的FileNames屬性的順序。 相反,你可以輕鬆地建立自己的文件的ListView在一個特定的文件夾,並通過自己的跟蹤的項目的順序點擊添加和從List<string>

List<string> filesSelected = new List<string>(); 

假設除去它們,例如有一個ListView與這些屬性

// Set the view to show details. 
listView1.View = View.Details; 

// Display check boxes. 
listView1.CheckBoxes = true; 
listView1.FullRowSelect = true; 
listView1.MultiSelect = false; 

// Set the handler for tracking the check on files and their order 
listView1.ItemCheck += onCheck; 

// Now extract the files, (path fixed here, but you could add a 
// FolderBrowserDialog to allow changing folders.... 
DirectoryInfo di = new DirectoryInfo(@"d:\temp"); 
FileInfo[] entries = di.GetFiles("*.*"); 

// Fill the listview with files and some of their properties 
ListViewItem item = null; 
foreach (FileInfo entry in entries) 
{ 
    item = new ListViewItem(new string[] { entry.Name, entry.LastWriteTime.ToString(), entry.Length.ToString()}); 
    listView1.Items.Add(item); 
}    
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);       

// Create columns for the items and subitems. 
// Width of -2 indicates auto-size. 
listView1.Columns.Add("File name", -2, HorizontalAlignment.Left); 
listView1.Columns.Add("Last Write Time2", -2, HorizontalAlignment.Left); 
listView1.Columns.Add("Size", -2, HorizontalAlignment.Left); 

此時的oncheck事件處理程序可用於從跟蹤的文件列表中添加和刪除文件

void onCheck(object sender, ItemCheckEventArgs e) 
{ 
    if (e.Index != -1) 
    { 
     string file = listView1.Items[e.Index].Text; 
     if (filesSelected.Contains(file)) 
      filesSelected.Remove(file); 
     else 
      filesSelected.Add(file); 
    } 
} 
+0

謝謝史蒂夫,我認爲這是我可以用OpenFileDialog所允許的最好的方法。 –

相關問題