Walkthrough: Creating an Explorer Style Interface with the ListView and TreeView Controls Using the Designer

One of the benefits of Visual Studio 2005 is the ability to create professional-looking Windows Forms applications in a short of amount of time. A common scenario is creating a user interface (UI) with ListView and TreeView controls that resembles the Windows Explorer feature of Windows operating systems. Windows Explorer displays a hierarchical structure of the files and folders on a user's computer.


The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To create the form containing a ListView and TreeView control

  1. On the File menu, point to New, and then click Project.

  2. In the New Project dialog box, do the following:

    1. In the Project Types pane, choose either Visual Basic Projects or Visual C# Projects.

    2. In the Templates pane, choose Windows Application.

  3. Click OK. A new Windows Forms project is created.

  4. Add a SplitContainer control to the form and set its Dock property to Fill.

  5. Add an ImageList named imageList1 to the form and use the property browser to add two images: a folder and a document image, in that order.

  6. Add a TreeView control named treeview1 to the form, and position it on the left side of the SplitContainer control. In the property browser for treeView1 do the following:

    1. Set the Dock property to Fill.

    2. Set the ImageList property to imagelist1.

  7. Add a ListView control named listView1 to the form, and position it on the right side of the SplitContainer control. In the property browser for listview1 do the following:

    1. Set the Dock property to Fill.

    2. Set the View property to Details.

    3. Open the ColumnHeader Collection Editor by clicking the ellipses (VisualStudioEllipsesButton screenshot) in the Columns property. Add three columns and set their Text property to Name, Type, and Last Modified, respectively. Click OK to close the dialog box.

    4. Set the SmallImageList property to imageList1.

  8. Implement the code to populate the TreeView with nodes and subnodes. The example code reads from the file system and requires the existence of two icons, folder.ico and doc.ico that were previously added to imageList1.

    private void PopulateTreeView()
        TreeNode rootNode;
        DirectoryInfo info = new DirectoryInfo(@"C:\Documents and Settings");
        if (info.Exists)
            rootNode = new TreeNode(info.Name);
            rootNode.Tag = info;
            GetDirectories(info.GetDirectories(), rootNode);
    private void GetDirectories(DirectoryInfo[] subDirs, 
        TreeNode nodeToAddTo)
        TreeNode aNode;
        DirectoryInfo[] subSubDirs;
        foreach (DirectoryInfo subDir in subDirs)
            aNode = new TreeNode(subDir.Name, 0, 0);
            aNode.Tag = subDir;
            aNode.ImageKey = "folder";
            subSubDirs = subDir.GetDirectories();
            if (subSubDirs.Length != 0)
                GetDirectories(subSubDirs, aNode);
  9. Since the previous code uses the System.IO namespace, add the appropriate using or import statement at the top of the form.

    using System.IO;
  10. Call the set-up method from the previous step in the form's constructor or Load event-handling method.

    public Form1()
  11. Handle the NodeMouseClick event for treeview1, and implement the code to populate listview1 with a node's contents when a node is clicked.

    void treeView1_NodeMouseClick(object sender,
        TreeNodeMouseClickEventArgs e) 
        TreeNode newSelected = e.Node;
        DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
        ListViewItem.ListViewSubItem[] subItems;
        ListViewItem item = null;
        foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
            item = new ListViewItem(dir.Name, 0);
            subItems = new ListViewItem.ListViewSubItem[]
                {new ListViewItem.ListViewSubItem(item, "Directory"), 
                 new ListViewItem.ListViewSubItem(item, 
        foreach (FileInfo file in nodeDirInfo.GetFiles())
            item = new ListViewItem(file.Name, 1);
            subItems = new ListViewItem.ListViewSubItem[]
                { new ListViewItem.ListViewSubItem(item, "File"), 
                 new ListViewItem.ListViewSubItem(item, 

    If you are using C#, make sure you have the NodeMouseClick event associated with its event-handling method.

    this.treeView1.NodeMouseClick += 
        new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);

You can now test the form to make sure it behaves as expected.

To test the form

  • Press F5 to run the application.

    You will see a split form containing a TreeView control that displays a directory labeled c:\Documents and Settings on the left side, and a ListView control on the right side with three columns. You can traverse the TreeView by selecting directory nodes, and the ListView is populated with the contents of the selected directory.

This application gives you an example of a way you can use TreeView and ListView controls together. For more information on these controls, see the following topics:

Community Additions