1

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...?

1
  • 1
    What kind of application is this? Windows Forms? ASP.NET? WPF? Commented Dec 1, 2010 at 2:58

2 Answers 2

3

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));
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

For more information and a couple of different strategies on shortening paths, check out Jeff's blog post on the subject. The essence is you can use either a regular expression or the PathCompactPathEx API to do this automatically.
@hemanth:thanx...how can I display only the selected file name?
You mean only the file(s) which are selected by user? Don't worry! FileNames property of OpenFileDialog control returns an array of file paths which were selected by user. If user selects only one file, this array will contain only one element.
You can use Path class in System.IO namespace to play with path elements. See updated code snippet...
2

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);
            }
        }

1 Comment

@pavan: thanks for that...infact i wanted to add just file names...not images...anyway that also helped...thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.