c# - ListView MouseClick event not triggered when clicking on empty areas -
i have listview control shows files inside folder in grid layout. , have context menu 3 items should behave this:
- if user right clicks on file in
listviewcontext menu show 3items - if user right clicks on empty space in
listviewfirstitemof context view should invisible.
this add events , code mouseclick event of listview:
this.listviewfiles.mouseclick += new system.windows.forms.mouseeventhandler(this.listviewfiles_mouseclick); private void listviewfiles_mouseclick(object sender, mouseeventargs e) { if (e.button == mousebuttons.right) { if (listviewfiles.focuseditem.bounds.contains(e.location) == true) { contextmenufileslistbox.items[0].visible = false; contextmenufileslistbox.items[1].visible = false; } else { contextmenufileslistbox.items[0].visible = true; contextmenufileslistbox.items[1].visible = true; } contextmenufileslistbox.show(cursor.position); } } what happens when right-click on empty area in listviewfiles_mouseclick event not triggered @ all. cannot bring invisible item in context menu.
the mouseclick won't work unless hit item label. mousedown nice , trigger when hitting subitem label or empty space (none).
checking hitting best done using listviewhittestinfo object.
here how should work:
private void listview1_mousedown(object sender, mouseeventargs e) { listviewhittestinfo hi = listview1.hittest(e.location); if (e.button == mousebuttons.right) { if (hi.location == listviewhittestlocations.none) { contextmenufileslistbox.items[0].visible = false; contextmenufileslistbox.items[1].visible = false; } else { contextmenufileslistbox.items[0].visible = true; contextmenufileslistbox.items[1].visible = true; } contextmenufileslistbox.show(cursor.position); } }
Comments
Post a Comment