Reputation:
I'm not sure what its called or what i should look up. I'm using a listbox and a ContentMenuStrip which i attached with listBox1.ContextMenuStrip = contextMenuStrip1;
I would like to have items in my listbox right click able and select things like 'remove'. Using what i have now I can click anywhere on the listbox and i apply to results to the currently selected item. Which is confusing and prone to errors.
How do i make right clicks actually map to elements my mouse is over and not show a right click menu when it isnt over an element (empty space in a listbox).
Upvotes: 2
Views: 270
Reputation: 3416
The easy solution (not completely what you requested I know), would be just to make sure an item is selected before allowing the menu to open.
You do this by registering for the ContextMenuStrip.Opening
event, and cancelling it if no item is selected.
If I think of something more clever I'll update :)
[EDIT]
OK cool, there's a IndexFromPoint
method on the ListBox
. You can use it to determine wether the mouse is over an item! Tell me if you need a code sample.
[EDIT2]
OK, OK, couldn't help myself.. here you go:
void ContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
if (listBox1.IndexFromPoint(listBox1.PointToClient(Cursor.Position)) == -1)
e.Cancel = true;
}
Upvotes: 2