Dark Knight
Dark Knight

Reputation: 3577

adding files to listview in C#

I have a Listview and an "ADD" button,when I click on ADD i should be able to browse the files in computer, select files and when click OK or the Open, the file list should be added in the listview...how to do that...is listview correct or any other alternative...?

Upvotes: 1

Views: 6844

Answers (2)

PawanS
PawanS

Reputation: 7193

If you want to do this in the designer, you can take the following steps to add the images to the ListView control:

  1. Switch to the designer, click on the ImageList component on the Component Tray, there will be a smart tag appear on the top-right corner of the ImageList.
  2. Click the smart tag, and click "Choose Images" on the pane.
  3. On the pop-up Image Collection Editor dialog, choose the images from the folder your want.
  4. Click OK to finish adding images to the ImageList.
  5. Click the ListView on the form, there will be a smart tag appear on the top-right corner.
  6. Click the smart tag, you will find there're three ComboBoxes there, choose a ImageList from the list as you want.
  7. Click the "Add items" option on the smart tag, a ListViewItem Collection Editor will appear, you can add items to the ListView, it's important here to set the ImageIndex or ImageKey property, or the image won't appear.
  8. Click OK to finish item editing, now you'll find the images are displayed on the ListView.

If you want to add the images to the ListView by code, you can do something like this

give the following code in addButton_click

        var fdlg = new OpenFileDialog();
        fdlg.Multiselect = true;
        fdlg.Title = "Select a file to add... ";
        fdlg.InitialDirectory = "C:\\";
        fdlg.Filter = "All files|*.*";
        fdlg.RestoreDirectory = true;
        if (fdlg.ShowDialog() == DialogResult.OK)
        {
            foreach (var files in fdlg.FileNames)
            {
                try
                {
                    this.imageList1.Images.Add(Image.FromFile(files));
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            this.listView1.View = View.LargeIcon;
            this.imageList1.ImageSize = new Size(32, 32);
            this.listView1.LargeImageList = this.imageList1;
            //or
            //this.listView1.View = View.SmallIcon;
            //this.listView1.SmallImageList = this.imageList1;
           for (int j = 0; j < this.imageList1.Images.Count; j++)
            {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = j;
                this.listView1.Items.Add(item);
            }
        }

Upvotes: 2

Hemant
Hemant

Reputation: 19846

ListView should be fine for file listing. Just be aware that longer file paths are difficult to see (have to horizontally scroll which is bad!) if you are gonna just add the full path to list. You can toy with the idea of other representation like:

File.Txt (C:\Users\Me\Documents)
C:\Users\..\File.Txt
etc

As far as doing it using code is concerned, you will need to use OpenFileDialog control to let user choose the files.

var ofd = new OpenFileDialog ();
//add extension filter etc
ofd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
if(ofd.ShowDialog() == DialogResult.OK)
{
    foreach (var f in openFileDialog1.FileNames)
    {
        //Transform the list to a better presentation if needed
        //Below code just adds the full path to list
        listView1.Items.Add (f);

        //Or use below code to just add file names
        //listView1.Items.Add (Path.GetFileName (f));
    }
}

Upvotes: 3

Related Questions