AddThis Social Bookmark Button

Print

Working with ListView
Pages: 1, 2

Using the LargeIcon View

Another view of the ListView control is LargeIcon. To use this view, you must set the View property of the ListView to View.LargeIcon. You must also construct an ImageList control and add one or more images to the ImageList control. In the code in Listing 2, an image (C:\temp\MyImage.bmp) is added to the ImageList imageList, which is then assigned to the LargeImageList of the ListView control.



Listing 2: Using the LargeIcon view


using System;
using System.Windows.Forms;
using System.Drawing;

public class ListView2 : Form {

  ListView listView = new ListView();

  public ListView2() {
    listView.Dock = DockStyle.Fill;
    PopulateListView();
    this.Controls.Add(listView);
    this.ClientSize = new Size(400, 200);
  }

  private void PopulateListView() {
    // Set the view to show details.
    listView.View = View.LargeIcon;
    ImageList imageList = new ImageList();
    imageList.Images.Add(
                       Bitmap.FromFile(@"C:\temp\MyImage.bmp")
                        );
     
    listView.LargeImageList = imageList;
 
    // Add columns, with listView.VIew = View.LargeIcon, 
    // the following 3 lines have no effect
    listView.Columns.Add("Author", 
                         -2, 
                         HorizontalAlignment.Center);
    listView.Columns.Add("Title", 
                         -2, 
                         HorizontalAlignment.Left);
    listView.Columns.Add("Price", 
                         -2, 
                         HorizontalAlignment.Left);


    ListViewItem item1 = new ListViewItem("Steve Martin");
    item1.SubItems.Add("Programming .NET");
    item1.SubItems.Add("39.95");
    item1.ImageIndex = 0;

    ListViewItem item2 = new ListViewItem("Irene Suzuki");
    item2.SubItems.Add("VB.NET Core Studies");
    item2.SubItems.Add("69.95");
    item2.ImageIndex = 0;

    ListViewItem item3 = new ListViewItem("Ricky Ericsson");
    item3.SubItems.Add("Passing Your .NET Exams");
    item3.SubItems.Add("19.95");
    item3.ImageIndex = 0;

    // Add the items to the ListView.
    listView.Items.AddRange(
                            new ListViewItem[] {item1, 
                                                item2, 
                                                item3}
                            );
  }


  public static void Main() {
    ListView2 form = new ListView2();
    Application.Run(form);
  }
}

After compilation, you can run the ListView2 class. It will give you the result like the one shown in Figure 2.

Figure 2
Figure 2: A ListView control with the LargeIcon view

Displaying A Directory's Contents Using ListView

As the last example, consider the code in Listing 3. It grabs the content of the current directory (the directory from which the .exe file is run) and displays the subdirectories and files in that directory. An ImageList called imageList is constructed and two images are added to it. Both images reside in the images directory of the application directory. The first one (File.gif) is added to each file item, and the second one (Folder.gif) is used for each folder item.

Listing 3: Using ListView to display a directory's contents


using System;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Text;

public class ListView3 : Form {

  ListView listView = new ListView();

  public ListView3() {
    listView.Dock = DockStyle.Fill;
    PopulateListView();
    this.Controls.Add(listView);
    this.ClientSize = new Size(400, 200);
    listView.SelectedIndexChanged += 
                new EventHandler(listView_SelectedIndexChanged);
  }

  private void listView_SelectedIndexChanged(Object sender, 
                                            EventArgs e) {
    ListView.SelectedListViewItemCollection 
                        selectedItems = listView.SelectedItems;
    
    StringBuilder sb = new StringBuilder(1024);
    sb.Append("You selected the following item(s).\n");
    
    int count = selectedItems.Count;
    for (int i=0; i<count; i++) {
      sb.Append("\t").Append(selectedItems[i].Text)
                               .Append("\n");
    }
    MessageBox.Show(sb.ToString());
  }
  
  private void PopulateListView() {
    // Set the view to show details.
    listView.View = View.Details;
    ImageList imageList = new ImageList();
    try {
      imageList.Images.Add(
                        Bitmap.FromFile("./images/File.gif")
                          );
      imageList.Images.Add(
                        Bitmap.FromFile("./images/Folder.gif")
                          );
    }
    catch (FileNotFoundException) {}

    listView.SmallImageList = imageList;
 
    //Add columns, 
    listView.Columns.Add("Name", 
                         -2, 
                         HorizontalAlignment.Left);
    listView.Columns.Add("Size", 
                         -2, 
                         HorizontalAlignment.Right);
    listView.Columns.Add("Type", 
                         -2, 
                         HorizontalAlignment.Left);
    listView.Columns.Add("Modified", 
                         -2, 
                         HorizontalAlignment.Left);

    String currentDirectory = Directory.GetCurrentDirectory();
    this.Text = currentDirectory;
    ListViewItem item;

    String[] directories = 
                   Directory.GetDirectories(currentDirectory);
    int length = directories.Length;
    for (int i=0; ilength; i++) {
      item = new ListViewItem(Path.GetFileName(directories[i]),
                              1);
      item.SubItems.Add("");
      item.SubItems.Add("File Folder");
      item.SubItems.Add(
          Directory.GetLastAccessTime(directories[i]).ToString()
                       );
      listView.Items.Add(item);
    }

    String[] files = Directory.GetFiles(currentDirectory);
    length = files.Length;
    for (int i=0; i<length; i++) {
      item = new ListViewItem(Path.GetFileName(files[i]), 0);
      //get file length
      FileInfo fi = new FileInfo(files[i]);
      item.SubItems.Add(Convert.ToString(fi.Length));
      item.SubItems.Add("Document");
      item.SubItems.Add(
                      File.GetLastWriteTime(files[i]).ToString()
                       );
      listView.Items.Add(item);
    }
  }
  public static void Main() {
    ListView3 form = new ListView3();
    Application.Run(form);
  }
}
The majority of the work is done in the PopulateListView method. First, it sets the View property of the ListView with View.Details:

    listView.View = View.Details;

Then, it constructs an ImageList object and adds two images to it:


    ImageList imageList = new ImageList();
    try {
      imageList.Images.Add(
                Bitmap.FromFile("./images/File.gif")
                          );
      imageList.Images.Add(
                Bitmap.FromFile("./images/Folder.gif")
                          );
    }
    catch (FileNotFoundException) {}

The ImageList object is then assigned to the SmallImageList property of the ListView so that each item will have an image.


    listView.SmallImageList = imageList;

Next, it adds four columns: Name, Size, Type, and Modified.


    //Add columns, 
    listView.Columns.Add("Name", 
                         -2, 
                         HorizontalAlignment.Left);
    listView.Columns.Add("Size", 
                         -2, 
                         HorizontalAlignment.Right);
    listView.Columns.Add("Type", 
                         -2, 
                         HorizontalAlignment.Left);
    listView.Columns.Add("Modified", 
                         -2, 
                         HorizontalAlignment.Left);

Up to this point, we're done with the ListView object. Obtaining the current directory's content starts by invoking the static GetCurrentDirectory method of the System.IO.Directory class. This method returns a string containing the path to the current directory.


    String currentDirectory = Directory.GetCurrentDirectory();

This is then assigned to the form's Text property so that the current directory is displayed as the form's title.


    this.Text = currentDirectory;

The list of subdirectories can be obtained from the GetDirectories static method of the Directory class, passing the path to the directory. Once the list of directories is returned as an array of strings, we iterate each item in the collection, constructing a ListViewItem object and adding it to the ListView control.


    String[] directories = 
                     Directory.GetDirectories(currentDirectory);
    int length = directories.Length;
    for (int i=0; i<length; i++) {
      item = new ListViewItem(Path.GetFileName(directories[i]), 
                              1);
      item.SubItems.Add("");
      item.SubItems.Add("File Folder");
      item.SubItems.Add(
        Directory.GetLastAccessTime(directories[i]).ToString()
                       );
      listView.Items.Add(item);
    }

Next, we do the same with the list of files, obtained from the GetFiles static method of the System.IO.Directory class.


    String[] files = Directory.GetFiles(currentDirectory);
    length = files.Length;
    for (int i=0; i<length; i++) {
      item = new ListViewItem(Path.GetFileName(files[i]), 0);
      //get file length
      FileInfo fi = new FileInfo(files[i]);
      item.SubItems.Add(Convert.ToString(fi.Length));
      item.SubItems.Add("Document");
      item.SubItems.Add(
                      File.GetLastWriteTime(files[i]).ToString()
                       );
      listView.Items.Add(item);
    }
  }

We also capture the ListView control's SelectedIndexChanged event and wires the event with the listView_SelectedIndexChanged event handler.


listView.SelectedIndexChanged += 
                new EventHandler(listView_SelectedIndexChanged);

Every time the user selects a new item or multiple items, the SelectedIndexChanged event will be triggered. What the event handler of this event (listView_SelectedIndexChanged) does is get the ListView.SelectedListViewItemCollection from the SelectedItems property.


    ListView.SelectedListViewItemCollection selectedItems = 
                                         listView.SelectedItems;

Then, it iterates the collection to build a StringBuilder, iterating each of the items.


    StringBuilder sb = new StringBuilder(1024);
    sb.Append("You selected the following item(s).\n");
    
    int count = selectedItems.Count;
    for (int i=0; i<count; i++) {
      sb.Append("\t").Append(selectedItems[i].Text)
                               .Append("\n");
    }
    MessageBox.Show(sb.ToString());
  }

A screenshot of the run application is given in Figure 3.

Figure 3
Figure 3: Displaying the current directory's content

The complete application for this example can be downloaded here

Budi Kurniawan is a senior J2EE architect and author.


Return to ONDotnet.com