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:

  1. if user right clicks on file in listview context menu show 3 items
  2. if user right clicks on empty space in listview first item of 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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -