1

Can anyone guide me as I am facing this issue?

I have a app in which I can bring items through a browse button, now when a user selects multiple files , though it selects them and add them (because multiselect = true) but it is selecting the last selected item at the top why?

Therefore causing the flow to be wrong.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
user1100199
  • 53
  • 2
  • 4
  • 12
  • 2
    For what it's worth, this behavior comes from the common Open File dialog (part of Windows), not from C#'s abstraction of it. That dialog has been behaving this way since Windows 2000, and maybe even before. The reordering probably comes from an internal implementation detail which might not be easy or desirable for Microsoft to fix. – Frédéric Hamidi Jan 30 '12 at 13:17
  • Is the array in reverse order?? – craig1231 Jan 30 '12 at 13:17
  • @craig, nope, but the last item selected is always reordered at the top of the list. – Frédéric Hamidi Jan 30 '12 at 13:18
  • 1
    The order is random anyway, whatever the "Arrange by" selection that was used which you cannot control or see. You need to treat the selection as a set, not a list. Or sort it if that makes sense. – Hans Passant Jan 30 '12 at 13:49

3 Answers3

1

Without seeing the relevant code, the issue could be a different one, but just to make you aware, there are two ways of getting the output: either using FileName, or FileNames.

If you enable MultiSelect yet only look at the FileName property, you will only get the name of one file,

This property can only be the name of one selected file. If you want to return an array containing the names of all selected files in a multiple-selection dialog box, use FileNames.

Well, two ways is a simplification really, because you also have SafeFileName and SafeFileNames. I'll leave the research into such as an exercise for the reader.

It's possible I have misinterpreted your problem based on comments, but let's see.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • I don't think this is the problem. – svick Jan 30 '12 at 13:25
  • @svick Possibly. The _'item at the top'_ is nagging me to think not. However, without information on ordering requirements (as noted by you), I'm not sure what advice they would be looking for. It would worry me to think the OP couldn't figure how to order them in any way otherwise (with the question not being stated). – Grant Thomas Jan 30 '12 at 13:27
  • @ Mr. Disappointment ..i have been using SafeFileNames when i require multiple values from the box but my major concern is to get the order correct in which it is being selected from the openfiledialgue.That is if i select 1,2,3,4,5(throuh multiselect ) then it should reflect 1,2,3,4,5 as the order and not 5,4,1,2,4. – user1100199 Jan 31 '12 at 08:47
  • can anyone suggest on the same ...as i have been looking and working on this from a long time now and haven't been able to find the correct solution to it.. – user1100199 Feb 03 '12 at 09:45
1

You didn't say why the order matters for you and if you really need it in the same order as the user selected the files.

But if you don't, you could simply sort the selected files by their name: this gives you predictable sort order that makes sense.

svick
  • 236,525
  • 50
  • 385
  • 514
1

The dialog returns the focused filename (the one with the dotted-line border, which is the one you clicked most recently) first. So if you click on one filename, and then Shift+Click on one lower in the list, the last one becomes the focused one and it's first in the list, followed by the others in order.

If you do it the other way around -- click one, then Shift+click one higher in the list -- then they'll be listed in the order you expect.

Same thing if you don't Shift+click, but instead drag a rectangle around the files you want to select. That doesn't change the focus (which will stay on the first filename in the list), so they're in the order they're shown in the list.

There's more to it if the user starts using the keyboard (Shift+Up/Down, Ctrl+Space, Ctrl+Up/Down), or selecting disjoint ranges using Ctrl+click, but the bottom line is, the order is all up to the user, and how they go about selecting the files.

But why on earth do you care what order the files are listed? You should really just be treating this as an unordered list (because there's really no good way for the user to specify the order -- that's not the intent of the dialog, so it was never designed to make it easy to order your selection). If you want to show the filenames in alphabetical order, or some other kind of order, then just sort the list you get back.

Joe White
  • 94,807
  • 60
  • 220
  • 330