Search code examples
c#.netwinformslistviewlistviewitem

How to remove 1px space between ListViewItem


In ListView between ListViewItems have 1px space and it causing me to miss click between them frequently. This only happens if ListViewItem in ListViewGroup.

I recorded video to show it: https://jaex.getsharex.com/2022/06/pBBk1aXmD6.mp4

In video I can click between two items which makes ListViewItem unselected. This problem not happens if I don't use ListViewGroup.


Solution

  • I'm using this workaround now:

        private void lvMain_MouseUp(object sender, MouseEventArgs e)
        {
            if (lvMain.SelectedItems.Count == 0 && (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right))
            {
                ListViewItem lvi = lvMain.GetItemAt(e.X, e.Y);
    
                if (lvi == null)
                {
                    // Workaround for 1px space between items
                    lvi = lvMain.GetItemAt(e.X, e.Y - 1);
                }
    
                if (lvi != null)
                {
                    lvi.Selected = true;
                }
            }
        }
    

    If clicked to empty space between two items then checking one pixel top of this position and if item exist in this position then selecting it.