prevent listview to lose selected item

后端 未结 6 1821
失恋的感觉
失恋的感觉 2021-01-18 03:43

I\'m currently working on a listview in winform c# and everytime I click on an empty space on the listview, the selected item is lost.

6条回答
  •  -上瘾入骨i
    2021-01-18 04:13

    You have to inherit from the ListView class and do some low-level message processing

    class ListViewThatKeepsSelection : ListView
    {
        protected override void WndProc(ref Message m)
        {
            // Suppress mouse messages that are OUTSIDE of the items area
            if (m.Msg >= 0x201 && m.Msg <= 0x209)
            {
                Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
                var hit = this.HitTest(pos);
                switch (hit.Location)
                {
                    case ListViewHitTestLocations.AboveClientArea:
                    case ListViewHitTestLocations.BelowClientArea:
                    case ListViewHitTestLocations.LeftOfClientArea:
                    case ListViewHitTestLocations.RightOfClientArea:
                    case ListViewHitTestLocations.None:
                        return;
                }
            }
            base.WndProc(ref m);
        }
    }
    

提交回复
热议问题