Question

I want to handle a group header click event. I used information from this post to achieve my goal. The application properly recognizes mouse click on the group header, the problem actually is, as the title states, that the group id returned by SendMessage method is wrong for some of the groups.

My ListView (on which I tested the code) looks like this:

Group 1
  Item 1.1
  Item 1.2
Group 2
  Item 2.1
  Item 2.2
Group 3
  Item 3.1

Basically, it works fine until I click on the 2nd or 3rd group. When clicking group headers SendMethod returns the following values: 0 for Group 1, 2 for Group 2 and 3 for Group 3.

Here's the WndProc method implementation:

protected override void WndProc(ref Message m)
    {
        bool passMessage = true;

        if (m.Msg == WM_RBUTTONDOWN)
        {
            Point hitPoint = LParamToPoint(m.LParam);
            LVHITTESTINFO lvHitTestInfo = new LVHITTESTINFO();
            lvHitTestInfo.pt.x = hitPoint.X;
            lvHitTestInfo.pt.y = hitPoint.Y;

            int rtn = SendMessage(listView.Handle, LVM_SUBITEMHITTEST, -1, ref lvHitTestInfo);
            if (rtn != -1)
            {
                if (((lvHitTestInfo.flags & LVHITTESTFLAGS.LVHT_EX_GROUP_HEADER) == LVHITTESTFLAGS.LVHT_EX_GROUP_HEADER))
                {
                    passMessage = false;

                    string groupName;
                    if (rtn == listView.Groups.Count)
                        groupName = "default";
                    else
                        groupName = listView.Groups[rtn].Header;
                    GroupClicked.Invoke(listView, new ListViewInterceptorEventArgs(rtn, groupName));
                }
            }
        }

        if (passMessage)
            base.WndProc(ref m);
    }

I want to display content related to the chosen group, so I need to know which one was clicked. I'm using Windows 8. If anyone could direct me to a solution, I'll be very thankful. If you need any other information, please let me know and I will provide you with it.

P.S. In my app I add groups dynamically, like this:

                ListViewGroup contactsGroup = new ListViewGroup(group.id, group.name);
                contactsGroup.Name = group.id;
                contactsGroup.HeaderAlignment = HorizontalAlignment.Center;
                lvContacts.Groups.Add(contactsGroup);
                for (int i = 0; i < group.users.Count; i++)
                {
                    ListViewItem lvi = new ListViewItem(group.users[i]);
                    lvi.SubItems.Add(group.userStatus[i] ? "ONLINE" : "offline");
                    lvi.Group = contactsGroup;
                    lvContacts.Items.Add(lvi);
                }

The same WndProc method implementation works just fine in another program, which I wrote just to test this and in which I added the groups via the designer, though.

Était-ce utile?

La solution

June 2019 : This is still an issue an Microsoft clearly hasn't fixed it yet !

But thanks to David Amey we have a fix. You first need to create and add all your groups to the listview

Dim MyLvg As ListViewGroup = New ListViewGroup With {.Header = "MyGroup1", .Name = "MyGroup1"}
MyLsv.Groups.Add(MyLvg)

Only after that will you be able to add items.


I also figured out, at this stage it won't work if you declare a new ListViewItem from scratch and add it, but rather here you need to go through your existing ListView instance to create it. Then just link your Lvi to your Lsv and you're all set.

Dim MyLvi As ListViewItem = MyLsv.Items.Add("This is my new item")
MyLvi.Group = MyLvg

On a final note, let me add that I've tested 6 ways to link ListViewItems to ListViewGroups and all were working ok

MyLvi.Group = MyLvg
MyLvi.Group = MyLsv.Groups(0)
MyLvi.Group = MyLsv.Groups("MyGroup1")

MyLvg.Items.Add(MyLvi)
MyLsv.Groups(0).Items.Add(MyLvi)
MyLsv.Groups("MyGroup1").Items.Add(MyLvi)

This could seems logical, but we still don't get those bugs so I preferred to double check.

Using this way of creating and linking Groups and Items will definitely ensure you to avoid this "Bad Group Id" problem from LVHITTESTINFO

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top