Windows 7 TreeView and ListView


The default Tree and List view controls in windows forms look rather dated. In an earlier post, I demonstrated just how easy it was to change the theme of the TreeView control and ListView controls so they look like Vista’s explorer.

The same code can be used to attain the Windows 7 Tree and List view controls look and feel.

TreeAndList

Here I have a TreeView with an ImageList, and a ListView with an ImageList that has some large images, and am populating it when the form loads. If you would like to go further with the other controls (buttons etc.), there is a very good article on codeproject.com worth checking out.

Visual Basic (c# example beneath)

Imports System.Runtime.InteropServices

Public Class Form1

    Private random As Random

 

    Public Sub New()

        Me.Font = System.Drawing.SystemFonts.IconTitleFont

 

        InitializeComponent()

 

        SetWindowTheme(treeView1.Handle, "explorer", Nothing)

        SetWindowTheme(listView1.Handle, "explorer", Nothing)

 

        Me.treeView1.HotTracking = True

        Me.treeView1.FullRowSelect = True

    End Sub

    ”’ <summary>

    ”’ Native interop method, be sure to include the  System.Runtime.InteropServices

    ”’ name space

    ”’ </summary>

    ”’ <param name="hWnd"></param>

    ”’ <param name="appName"></param>

    ”’ <param name="partList"></param>

    ”’ <returns></returns>

    <DllImport("uxtheme.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)> _

    Private Shared Function SetWindowTheme(ByVal hWnd As IntPtr, ByVal appName As String, ByVal partList As String) As Integer

    End Function

 

 

    ”’ <summary>

    ”’ Using an image collection in an ImageList component, fill the

    ”’ ListView control with 50 random images when the form loads

    ”’ </summary>

    ”’ <param name="sender"></param>

    ”’ <param name="e"></param>

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        random = New Random()

 

        Me.treeView1.ExpandAll()

        Me.listView1.LargeImageList = Me.imageList2

        Me.listView1.BeginUpdate()

 

        For i As Integer = 0 To 49

            Dim item As New ListViewItem()

 

            item.ImageIndex = random.[Next](0, imageList2.Images.Count)

 

            Dim name As String = DirectCast(item.Name, String)

 

            item.Text = "Item " & name

 

            Me.listView1.Items.Add(item)

        Next

 

        Me.listView1.EndUpdate()

    End Sub

 

 

End Class

C#

using System;

using System.Windows.Forms;

using System.Runtime.InteropServices;

 

namespace WindowsSevenTreeViewAndListView

{

    public partial class Form1 : Form

    {

        Random random;

 

        public Form1()

        {

            this.Font = System.Drawing.SystemFonts.IconTitleFont;

 

            InitializeComponent();

 

            SetWindowTheme(treeView1.Handle, "explorer", null);

            SetWindowTheme(listView1.Handle, "explorer", null);  

 

            this.treeView1.HotTracking = true;

            this.treeView1.FullRowSelect = true;

        }

        /// <summary>

        /// Native interop method, be sure to include the  System.Runtime.InteropServices

        /// name space

        /// </summary>

        /// <param name="hWnd"></param>

        /// <param name="appName"></param>

        /// <param name="partList"></param>

        /// <returns></returns>

        [DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]

        private static extern int SetWindowTheme(IntPtr hWnd, string appName, string partList);

 

        /// <summary>

        /// Using an image collection in an ImageList component, fill the

        /// ListView control with 50 random images when the form loads

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void Form1_Load(object sender, EventArgs e)

        {

            random = new Random();

 

            this.treeView1.ExpandAll();

            this.listView1.LargeImageList = this.imageList2;          

            this.listView1.BeginUpdate();

 

            for (int i = 0; i < 50; i++)

            {

                ListViewItem item = new ListViewItem();

 

                item.ImageIndex = random.Next(0, imageList2.Images.Count);

 

                string name = (string)item.Name;

 

                item.Text = "Item " + name;

 

                this.listView1.Items.Add(item);

            }

 

            this.listView1.EndUpdate();

        }

    }

}

4 thoughts on “Windows 7 TreeView and ListView

  1. Pingback: Vista Treeview and Listview « Ira Lukhezo’s blog

  2. HI,
    The issue I am facing is that TreeView is populated fine, but on loast focus from the tree view the selected item is not correctly themed.. its having the back color and other properties just the same as other nodes and hence not able to distinguish between the Selected Node and non selected ones.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s