WinForms ListView DoubleClick Event for Empty Lists
The WinForms ListView control fires double click events only if the user clicked on a selected item. This is also documented on the MSDN site: “The mouse pointer must be over a child object (TreeNode or ListViewItem).” (Control.DoubleClick Event).
Sometimes it is useful to perform actions on double click events for empty ListView controls, too.
To achieve this you have to implement a custom ListView control which inherits from the original WinForms ListView control:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using System; using System.Security.Permissions; using System.Windows.Forms; internal class CustomListView : ListView { private const int WM_LBUTTONDBLCLK = 515; private bool doubleClickFired; protected override void OnDoubleClick(EventArgs e) { base .OnDoubleClick(e); this .doubleClickFired = true ; } [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] protected override void WndProc( ref Message m) { this .doubleClickFired = false ; base .WndProc( ref m); if (m.Msg == WM_LBUTTONDBLCLK && ! this .doubleClickFired) { this .OnDoubleClick(EventArgs.Empty); } } } |
This custom control checks whether a double click event was triggered for double clicks of the left mouse button on the control. If this is not the case, it triggers a double click event “manually”.