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

One of the benefits of Visual Studio 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.

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 categories, choose either Visual Basic or Visual C#.

    2. In the list of templates, choose Windows Forms 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 Properties window to add two images: a folder image 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 Properties window 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 Properties window 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 (The Ellipsis button (...) in the Properties window of Visual Studio.) 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. Add this code to the Form1 class.

    private void PopulateTreeView()
        TreeNode rootNode;
        DirectoryInfo info = new DirectoryInfo(@"../..");
        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);
    Private Sub PopulateTreeView() 
        Dim rootNode As TreeNode
        Dim info As New DirectoryInfo("../..")
        If info.Exists Then
            rootNode = New TreeNode(info.Name)
            rootNode.Tag = info
            GetDirectories(info.GetDirectories(), rootNode)
        End If
    End Sub
    Private Sub GetDirectories(ByVal subDirs() As DirectoryInfo, _
        ByVal nodeToAddTo As TreeNode)
        Dim aNode As TreeNode
        Dim subSubDirs() As DirectoryInfo
        Dim subDir As DirectoryInfo
        For Each subDir In subDirs
            aNode = New TreeNode(subDir.Name, 0, 0)
            aNode.Tag = subDir
            aNode.ImageKey = "folder"
            subSubDirs = subDir.GetDirectories()
            If subSubDirs.Length <> 0 Then
                GetDirectories(subSubDirs, aNode)
            End If
        Next subDir
    End Sub
  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;
    Imports System.IO
  10. Call the set-up method from the previous step in the form's constructor or Load event-handling method. Add this code to the form constructor.

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

    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,
    Private Sub treeView1_NodeMouseClick(ByVal sender As Object, _
        ByVal e As TreeNodeMouseClickEventArgs) _
            Handles treeView1.NodeMouseClick
        Dim newSelected As TreeNode = e.Node
        Dim nodeDirInfo As DirectoryInfo = _
        CType(newSelected.Tag, DirectoryInfo)
        Dim subItems() As ListViewItem.ListViewSubItem
        Dim item As ListViewItem = Nothing
        Dim dir As DirectoryInfo
        For Each dir In nodeDirInfo.GetDirectories()
            item = New ListViewItem(dir.Name, 0)
            subItems = New ListViewItem.ListViewSubItem() _
                {New ListViewItem.ListViewSubItem(item, "Directory"), _
                New ListViewItem.ListViewSubItem(item, _
        Next dir
        Dim file As FileInfo
        For Each file In nodeDirInfo.GetFiles()
            item = New ListViewItem(file.Name, 1)
            subItems = New ListViewItem.ListViewSubItem() _
                {New ListViewItem.ListViewSubItem(item, "File"), _
                New ListViewItem.ListViewSubItem(item, _
        Next file
    End Sub

    If you are using C#, make sure you have the NodeMouseClick event associated with its event-handling method. Add this code to the form constructor.

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

Testing the Application

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 your project directory 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.

Next Steps

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:

See also