Winforms ListView - Stop automatically checking when double clicking

后端 未结 5 1460
粉色の甜心
粉色の甜心 2021-02-19 04:15

How do I make a listview not automatically check an item when I double click it?

I can try hooking into the MouseDoubleClick event, and set the Checked property to false

5条回答
  •  离开以前
    2021-02-19 04:35

    If you don't want to turn of the DoubleClick messages completely, but just turn off the autocheck behaviour. You can instead do the following:

    public class NoDoubleClickAutoCheckListview : ListView
    {
        private bool checkFromDoubleClick = false;
    
        protected override void OnItemCheck(ItemCheckEventArgs ice)
        {
            if (this.checkFromDoubleClick)
            {
                ice.NewValue = ice.CurrentValue;
                this.checkFromDoubleClick = false;
            }
            else
                base.OnItemCheck(ice);
        }
    
        protected override void OnMouseDown(MouseEventArgs e)
        {
            // Is this a double-click?
            if ((e.Button == MouseButtons.Left) && (e.Clicks > 1)) {
                this.checkFromDoubleClick = true;
            }
            base.OnMouseDown(e);
        }
    
        protected override void OnKeyDown(KeyEventArgs e)
        {
            this.checkFromDoubleClick = false;
            base.OnKeyDown(e);
        }
    }
    

提交回复
热议问题