
I wanted to create new item in listview after doubleclick on the blank area. Unfortunately this event is not caught by standard events in .Net by default :(
There is work around: the easiest thing to do would be to create a custom class that inherits from ListView, then override WndProc to look for double clicks, and when they occur call the OnMouseDoubleClick method, which will fire the MouseDoubleClick event for the ListView in your form's code.
Add a new class to your project and call it ListViewEx. Then paste in this code:
class ListViewEx : System.Windows.Forms.ListView
{
private const int WM_LBUTTONDBLCLK = 0x203; //LEFT MOUSE BUTTON
private const int WM_RBUTTONDBLCLK = 0x206; //RIGHT MOUSE BUTTON
override protected void WndProc(ref Message m)
{
if ((m.Msg == WM_LBUTTONDBLCLK) || (m.Msg == WM_RBUTTONDBLCLK))
base.OnMouseDoubleClick(MouseEventArgs.E
else
base.WndProc(ref m);
}
}
In your forms code, if you need to tell the difference of if they double clicked the blank area or if they double clicked on an item, you just test for selected items. Like this:
private void MyListView_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (MyListView.selectedItems.Count == 0)
{
// Double Click Occured in Blank Area
}
else
{
// Double Click Occured on Item
}
}
![]() | You are viewing Log in Create a LiveJournal Account Learn more | |||