openfiledialogue in c# is selecting multiple values but its selecting the last selecteditem as the first

592 views Asked by At

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.

3

There are 3 answers

4
Grant Thomas On BEST ANSWER

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.

0
Joe White On

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.

0
svick On

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.