In my project I have a ListView
and I would like to open my ContextMenuStrip
when I clicked right button in the large icon. I tried many things but I am unsuccessful. When I right click inside of ListView
the ContextMenuStrip
opens, but I want to see just when I right clicked the large icon.
Also I need to help about get the clicked icon's name (properties).
This should work
private void listView1_MouseClick(object sender, MouseEventArgs e) { ListView listView = sender as ListView; if (e.Button == System.Windows.Forms.MouseButtons.Right) { ListViewItem item = listView.GetItemAt(e.X, e.Y); if (item != null) { item.Selected = true; contextMenuStrip1.Show(listView , e.Location); } } }
Search the listview item on mouse click location. If it is there, show the menu.........
Can you please try the following and let see wether it works or not... private void listView1_MouseClick(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) { if (listView1.FocusedItem.Bounds.Contains(e.Location) == true) { contextMenuStrip1.Show(Cursor.Position); } }
}
This is a quick and dirty solution; please do put more work into it than I did..
You should either code the
MouseDown
or theMouseUp
event: