displaying file order in a listbox

Posted on 2008-11-10
Medium Priority
Last Modified: 2012-05-05

I am using a button and a listbox in my winform.
When I click the button, a user can select multiple files at the same time.
However, always the last selected file goes to the top of the list in the listbox.
how can I list the files in the listbox in order to be selected?  For example, if I select the 3 doc files with following order:1.doc, 2.doc and 3.doc, and add all of them at the same time, the list of file should be displayed as 1.doc, 2.doc, and 3.doc from top to bottom.

The following codes are for the button.

        private void button1_Click_1(object sender, EventArgs e)
            dlg.Multiselect = true;
            if (dlg.ShowDialog() == DialogResult.OK)
                foreach (string fileName in dlg.FileNames)
                    textBox1.Text = fileName;
Question by:muffintwinkly

Accepted Solution

jlj1527 earned 200 total points
ID: 22927751
Because the OpenFileDialog always return you the results like this:
- The first element of FileNames array is the last file you selected
- The second, third .. element are ordered by the order you selected the files.
=> So your work is Adding the first Element of FileNames array at the end.

try this:
if (dlg.ShowDialog() == DialogResult.OK)
     for (int i = 1; i < dlg.FileNames.Length; i++)
             textBox1.Text = dlg.FileNames[i];
    textBox1.Text = dlg.FileNames[0];



Author Comment

ID: 22928177
thanks! it works great.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question