Get the item doubleclick event of listview

后端 未结 16 1451
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 02:17

What do I need to do in order to reference the double click event for a listview control?

16条回答
  •  死守一世寂寞
    2020-12-01 02:46

    Use the ListView.HitTest method

        private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var senderList  = (ListView) sender;
            var clickedItem = senderList.HitTest(e.Location).Item;
            if (clickedItem != null)
            {
                //do something
            }            
        }
    

    Or the old way

        private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var senderList  = (ListView) sender;                        
            if (senderList.SelectedItems.Count == 1 && IsInBound(e.Location, senderList.SelectedItems[0].Bounds))
            {
                //Do something
            }
        }
    
        public  bool IsInBound(Point location, Rectangle bound)
        {
            return (bound.Y <= location.Y && 
                    bound.Y + bound.Height >= location.Y &&
                    bound.X <= location.X && 
                    bound.X + bound.Width >= location.X);
        }
    

提交回复
热议问题