displaying file order in a listbox

Posted on 2008-11-10
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
    LVL 4

    Accepted Solution

    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

    thanks! it works great.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    ASP.NET C# export very large array to excel 9 45
    Query datatable in LINQ 6 27
    C# primary key 9 46
    Detect file exist or not 3 37
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now