How to get selected files from TOpenDialog exactly in the order they were selected in?

808 Views Asked by At

I use a TOpenDialog component in Delphi XE7, because I want to select one or more files. However, after I select them and click OK, the selected files are stored already sorted alphabetically, from A to Z, in the Files property, thing which I do not want. I didn't see any switches or options neither in the TOpenDialog control, nor in the TStrings type.

How can I make this component store the selected files exactly in the order that I want to?

2

There are 2 best solutions below

2
On BEST ANSWER

The underlying dialog box from the operating system doesn't keep track of that information (or if it does, it doesn't expose it in any way), and the wrapper class provided by Delphi doesn't synthesize it for you.

You can handle the OnSelectionChange event to deduce the selection order. Begin by creating your own ordered list to hold the selected files. When the event is triggered, inspect the dialog's Files property. Remove any entries from your internal list that aren't present in Files. For any items in Files that you don't already have, add them to the head of your list.

4
On

The system dialogs do not keep track of the order in which the items are selected. You have no way to get the system dialog to tell you that information. If you really need that then I see two options:

  1. Write your own dialog that does keep track of order of selection.
  2. Let the user specify order outside the file selection dialog.