0

My music player uses ListView in Details mode to show tracks, and ListViewGroup to group albums tracks together.

I have created event that removes all tracks in one album, when album's ListViewGroup is middle-clicked. Works fine if ListView is not scrolled down when removing tracks. If ListView is scrolled down, album will be removed, but large non-clickable empty space appears at the top of ListView, between headers and first group.

First image, 3 albums in ListView (scrolled up)

enter image description here


Second image, last album removed, everything's fine!

enter image description here


Ok, then ListView is scrolled down when removing one group of items

Third image, 3 albums in ListView (scrolled down)

enter image description here


Fourth image, last album removed, empty space appeared at the top, and last tracks if ListView is not showing at all!

enter image description here


I don't think this has anything to do with my code, but rather a bug in ListView? Anyone noticed same behavior? Is there any trick to prevent this from happening?


EDIT: added code which removes items (as user requested it)

List<ListViewItem> delItems = new List<ListViewItem>(); // creating list of items to be removed

foreach (ListViewGroup group in listView1.Groups)
{
    if (Convert.ToInt32(group.Tag) == groupNumber) // all groups have individual number in tag field
    {
        foreach (ListViewItem item in group.Items)
        {
            delItems.Add(item);
        }
    }
}

foreach (ListViewItem item in delItems)
{
    item.Remove();
}

EDIT2: added code to check if group was clicked

Since there is no real way to handle group click, I made it ugly way. But it works. First of all, I used MouseDown event in ListView. Then I check whether there is an item where user clicks. If user clicks group, then the item is null, and for loop increases i and checks if item can be found now. When eventually item found, we know that the item belongs to the group we clicked. So we get items group tag, where I keep record of what group it is.

private void listView1_MouseDown(object sender, MouseEventArgs e)
{
    ListViewItem item;

    for (int i = 0; i < 25; i++) // group height is not more that 25 pixels.
    {
        item = listView1.GetItemAt(e.X, e.Y + i);

        if (item != null)
        {
            if (i > 1) // bigger than 1, because there is 1 pixel gap between listviewitems when using groups
            {
                if (e.Button == MouseButtons.Middle) removePlaylistGroup(Convert.ToInt32(item.Group.Tag));
            }
            else
            {
                if (e.Button == MouseButtons.Middle) removePlaylistItem(item.Index);
            }
            break;
        }
    }
}
4

1 回答 1

0

所有这一切都发生了,因为从ListView显然会触发Form1_Layout事件中删除项目。如果表单布局发生变化,我会在那里listView1.BeginUpdate() ...调整 listView1 的大小。不知何故弄乱了我的listView。

于 2014-07-25T00:01:30.667 回答