Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Collapsible ListView

0.00/5 (No votes)
24 Oct 2018 2  
Collapsible ListView

Introduction

This is a short tip for a collapsible ListView.

Background

For some reasons, I had to create a Windows Form application and I needed to have a ListView control on it. Then I decided to make its groups collapsible and I found that although there are several pieces around the web, no one had put them into a class that a DEV can just put to use with no hooks and tricks around.

Using the Code

Without any further ado, here is the code. Just add it to your project and replace your existing ListView control references with this and all should work as before with collapsible groups.

public class CollapsibleListView : ListView
{
    private const int LVGF_STATE = 0x00000004;
    private const int LVGS_COLLAPSIBLE = 0x00000008;
    private const int LVM_FIRST = 0x1000;
    private const int LVM_SETGROUPINFO = (LVM_FIRST + 147);
    private const int LVM_INSERTGROUP = (LVM_FIRST + 145);
    private const int WM_LBUTTONUP = 0x0202;

    /// <summary>
    /// Intercepts <see cref="ListView.WndProc(ref Message)"/> calls
    /// </summary>
    /// <param name="message">Message</param>
    protected override void WndProc(ref Message message)
    {
        switch (message.Msg)
        {
            case WM_LBUTTONUP:
                base.DefWndProc(ref message);
                break;
            case LVM_INSERTGROUP:
                base.WndProc(ref message);
                var group = Marshal.PtrToStructure<LvGroup>(message.LParam);
                SetGroupCollapsible(group.iGroupId);
                return;
        }

        base.WndProc(ref message);
    }

    /// <summary>
    /// Sends the specified message to a window or windows.
    /// <seealso cref="https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-sendmessage"/>
    /// </summary>
    /// <param name="handle">A handle to the window 
    /// whose window procedure will receive the message.</param>
    /// <param name="message">The message to be sent.</param>
    /// <param name="wParam">Additional message-specific information.</param>
    /// <param name="lParam">Additional message-specific information.</param>
    /// <returns>The return value specifies the result of the message processing; 
    /// it depends on the message sent.</returns>
    [DllImport("user32.dll")]
    private static extern int SendMessage(IntPtr handle, int message, int wParam, IntPtr lParam);

    /// <summary>
    /// Represents native version of the <see cref="ListViewGroup"/> class
    /// <seealso cref="https://docs.microsoft.com/en-us/windows/desktop/api/commctrl/ns-commctrl-taglvgroup"/>
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    private struct LvGroup
    {
        public int cbSize;
        public int mask;
        [MarshalAs(UnmanagedType.LPTStr)]
        public string pszHeader;
        public int cchHeader;
        [MarshalAs(UnmanagedType.LPTStr)]
        public string pszFooter;
        public int cchFooter;
        public int iGroupId;
        public int stateMask;
        public int state;
        public int uAlign;
    }

    /// <summary>
    /// Updates a <see cref="ListViewGroup"/> item's state within current instance as collapsible
    /// </summary>
    /// <param name="groupItemIndex">Group item index</param>
    private void SetGroupCollapsible(int groupItemIndex)
    {
        LvGroup group = new LvGroup();
        group.cbSize = Marshal.SizeOf(group);
        group.state = LVGS_COLLAPSIBLE;
        group.mask = LVGF_STATE;
        group.iGroupId = groupItemIndex;

        IntPtr ip = IntPtr.Zero;
        try
        {
            ip = Marshal.AllocHGlobal(group.cbSize);
            Marshal.StructureToPtr(group, ip, false);
            SendMessage(Handle, LVM_SETGROUPINFO, groupItemIndex, ip);
        }
        finally
        {
            if (ip != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(ip);
            }
        }
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here