1

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?

Kromster
  • 7,181
  • 7
  • 63
  • 111
Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34

2 Answers2

3

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.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 2
    @whosrdaddy No that's not true. The file most recently added to the selection comes first. The rest of the files are not listed in order of selection. – David Heffernan May 12 '16 at 09:36
  • @whosrdaddy Then select d. For me I now have d a b c. – David Heffernan May 12 '16 at 11:53
  • I agree with @whosrdaddy here. In the edit box of the open dialog are shown the chosen files, in the order I had chosen them. However, this behavior doesn't translate to the Files property :( – Bogdan Doicin May 12 '16 at 14:17
  • @whosrdaddy Agrees with me now, having done more testing. It's just you that believes that the system respects the selection order in the edit box. Keep adding more and more files to the selection. – David Heffernan May 12 '16 at 14:39
3

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.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • Do you think it will work, given the fact that the **Files** property is automatically sorted? – Bogdan Doicin May 13 '16 at 11:22
  • Why would that affect anything? *Your* list won't be sorted alphabetically. Compare the contents of `Files` with the contents of your list and identify the differences. – Rob Kennedy May 13 '16 at 11:28