Reputation: 97
When using the OpenFileDialog with multiselect enabled, every time I select another files(using ctrl or shift + click), the most recently added files are inserted in the beginning of the File name textbox. Is there a way to change this and make them add to the end instead?
I am doing some work with the IFileDialog interface to customize it and file ordering is crucial to me.
I am working with .NET 4.5.
Edit: After doing some more experimenting, I am unsure of the ordering of the files after they are returned as well. It appears to be alphabetical. Can anyone verify this? I am having trouble finding good documentation/examples for this.
Upvotes: 1
Views: 127
Reputation: 216313
If you want to get your selected files in the exact order in which you click them, you cannot use the standard OpenFileDialog because you cannot control the order of the returned FileNames property.
Instead you could easily build your own ListView of files in a particular folder and keep track by yourself of the order of the items clicked adding and removing them from a List<string>
List<string> filesSelected = new List<string>();
Suppose, for example to have a ListView with these properties
// 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);
At this point the onCheck event handler could be used to add and remove the files from the list of tracked files
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);
}
}
Upvotes: 3