Click here to Skip to main content
Licence CPOL
First Posted 26 Nov 2008
Views 56,376
Downloads 1,317
Bookmarked 38 times

Add Group Collapse Behavior on a Listview Control

By | 26 Nov 2008 | Article
Group collapse behavior added to a listview control under Windows Vista

Introduction

ListView control is an important but complex control in the WinForm environment. Group behavior is added into this control, but unfortunately, we can't collapse or expand the group.

I'll show how to use some simple code to add collapse/expand behavior on the ListView control.

This is a sample image on Windows Vista and Windows Server 2008:

lvnormal.jpg

Default group without Collapse/Expand behavior

lvgroup.jpg

Group with Collapse/Expand behavior

Using the Code

  • Define ListView group struct wrapper:

    [StructLayout(LayoutKind.Sequential)] 
    public 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; 
    } 
  • Define the ENUM data:

    public enum GroupState 
    { 
            COLLAPSIBLE = 8, 
            COLLAPSED = 1, 
            EXPANDED = 0 
    } 
  • Interop for SendMessage function:

    [DllImport("user32.dll")] 
    static extern int SendMessage
    	(IntPtr window, int message, int wParam, IntPtr lParam);
  • Kernel method to set collapse/expand behavior on the ListView group:

    private void SetGroupCollapse(GroupState state)
    {
            for (int i = 0; i <= aoc.Groups.Count; i++){
                    LVGROUP group = new LVGROUP();
                    group.cbSize = Marshal.SizeOf(group);
                    group.state = (int)state; // LVGS_COLLAPSIBLE 
                    group.mask = 4; // LVGF_STATE 
                    group.iGroupId = i;
                    IntPtr ip = IntPtr.Zero;
                    try{
                            ip = Marshal.AllocHGlobal(group.cbSize);
                            Marshal.StructureToPtr(group, ip, true);
                            SendMessage(aoc.Handle, 0x1000 + 147, i, ip); // #define
                            LVM_SETGROUPINFO (LVM_FIRST + 147) 
                    }
                    catch (Exception ex){
                            System.Diagnostics.Trace.WriteLine
    			(ex.Message + Environment.NewLine + ex.StackTrace);
                    }
                    finally{
                            if (null != ip) Marshal.FreeHGlobal(ip);
                    }
           }
    } 

Points of Interest

#define LVM_SETGROUPINFO (LVM_FIRST + 147)
#define ListView_SetGroupInfo(hwnd, iGroupId, pgrp) \
SNDMSG((hwnd), LVM_SETGROUPINFO, (WPARAM)(iGroupId), (LPARAM)(pgrp)) 

The above is the definition from SDK file: commctrl.h.

Limitations

  • In the current version, the rightmost collapse/expand icon doesn't work. :(
  • The collapse/expand behavior only exist in Windows Vista and Win2k8 systems.

History

  • 27th November, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

charju

Software Developer

China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionDoesn't work Pinmemberheavylifting8:13 18 May '12  
AnswerRe: Doesn't work Pinmemberheavylifting8:16 18 May '12  
GeneralRepaint problem on Vista/Win7 [modified] PinmemberZhuJinYong18:01 26 May '11  
GeneralMy vote of 5 PinmemberChewsHumans11:48 21 Oct '10  
GeneralMy vote of 3 Pinmembero.olll20:36 12 Aug '10  
GeneralCorrections Pinmemberimess10:35 18 Jan '10  
GeneralRe: Corrections PinmemberMember 23549705:58 11 Feb '10  
GeneralMy vote of 1 Pinmemberts2210:25 24 Jul '09  
GeneralRe: My vote of 1 PinassociateScott Dorman5:26 15 Sep '09  
Generalgetting and setting the correct Group Id PinmemberPaw Jershauge20:37 18 May '09  
QuestionHow to do in VB.Net? PinmemberJimmy Bobby14:02 29 Apr '09  
AnswerRe: How to do in VB.Net? PinmemberPaw Jershauge22:52 13 May '09  
QuestionHow to make the click on the group arrow collapse the group... Pinmemberfmaeseele7:21 21 Jan '09  
AnswerRe: How to make the click on the group arrow collapse the group... Pinmemberrantinori15:54 2 May '09  
GeneralRe: How to make the click on the group arrow collapse the group... Pinmemberlewisv4:45 2 Jul '09  
GeneralRe: How to make the click on the group arrow collapse the group... Pinmemberrantinori9:20 2 Jul '09  
GeneralRe: How to make the click on the group arrow collapse the group... Pinmembermcarmonar8321:24 5 Nov '09  
GeneralRe: How to make the click on the group arrow collapse the group... Pinmemberrantinori7:28 6 Nov '09  
AnswerRe: How to make the click on the group arrow collapse the group... PinmemberZhuJinYong23:39 25 Jul '11  
Generalnot working under xp, VS 2005 Pinmemberzgelic11:40 9 Jan '09  
GeneralRe: not working under xp, VS 2005 PinmemberPaw Jershauge0:33 14 May '09  
GeneralI hadn't test the code under Windows XP or win2k Pinmembercharju15:00 27 Nov '08  
GeneralRe: I hadn't test the code under Windows XP or win2k Pinmembersroche4:17 26 Jan '09  
GeneralSource Code... PinmemberPaw Jershauge21:47 26 Nov '08  
GeneralRe: Source Code... Pinmembercharju22:53 26 Nov '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 27 Nov 2008
Article Copyright 2008 by charju
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid