Empty space appears at the top of ListView when removing Groups

1k views Asked by At

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;
        }
    }
}
1

There are 1 answers

8
darx On

All this was happening because removing items from ListView apparently fires Form1_Layout event. In there, I use listView1.BeginUpdate() ... to resize listView1, if the form layout changes. Somehow that messes up my listView.