displaying file order in a listbox

Hi,

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;
                    listBox1.Items.Add(Path.GetFileName(textBox1.Text));
                }
            }
        }
muffintwinklyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jlj1527Commented:
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];
             listBox1.Items.Add(Path.GetFileName(textBox1.Text));
     }
    textBox1.Text = dlg.FileNames[0];
     listBox1.Items.Add(Path.GetFileName(textBox1.Text));

}

 

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
muffintwinklyAuthor Commented:
thanks! it works great.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.