How can I tell a mouse right clicks over a listview item that's in

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How can I tell a mouse right clicks over a listview item that's in a
container panel. I only want to display a popup menu if the user right click
the mouse over an item on the Listview. I don't not want to display this
menu if a node selected on the listview but user right click over the empty
space of the panel that contain the listview.

The problem here is when I use the Listview's Click Event, I don't have ways
to verify if the right mouse button is clicked, but with this event only
occurs if the user is clicking on one of the item of the Listview control.

If I use the ListView's MouseClick event, then I can't verify if the right
click occurs over a Listview item. Can someone tell me how to solve this
problem? Thanks.
 
Try the MouseUp event instead. It has a MouseEventArgs as a parameter.
You can do a "if (e.Button == MouseButtons.Right)"...
 
hopefully this wiil help you.,

/*------------------------------- code
--------------------------------*/
if ((e.Button == MouseButtons.Right) &&
(this.lvClients.SelectedItems.Count != 0))// &&
//(this.lvClients.SelectedItems[0].Focused == true))
{

CheckMenuItemEnable();
this.contextMenu.Show(this.lvClients,new Point(e.X,e.Y));
}
Sincerely,
simida
 
Thank you Brendan but I'm not able to tell if the mouse was click over an
item on the Listview control. The listview control is in the right panel of
a container. If the user clicks on the blank space in the panel but not on
any particualr item on the listview control then I don't want to popup the
contextmenustrip.
 
I needed to do this a while ago, there may be a better way, but what I did
was this:

When the Right Mouse Button Is Pressed:

private void listBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
int itemPos = e.Y / listBox1.ItemHeight;
if (itemPos <= listBox1.Items.Count)
{
//Display your Context Menu.
// If you want the details of which item was clicked on its
available with listBox1.Items[itemPos].
}
}
}

Cheers,

Chris.
 
Back
Top