views:

69

answers:

4

I'm trying to have a contextmenustrip object show up on the coordinates that a right click occurs on a particular form object. I hooked into the forms CellMouseClick event and I can receive X,Y values for the event, but they seem to be relative to that particular control. For example, if I use contextmenu.Show(e.X, e.Y), it will show in the top hand corner of the screen, as opposed to where the mouse is on that form.

How can I accomplish what I am looking to do? If it helps, the form control I'm hooking into is DataGridView.

A: 

you need to check for left and top properties

x + control.left
y + control.top
Fredou
+2  A: 

I would just set the ContextMenuStrip property of the DataGridView to your ContextMenuStrip, then it will always appear where your right-click on the grid.

You can set this in code as well as in the Properties window of the designer.

smoore
+2  A: 

The DataGridView has a ContextMenu property that you can use for this.

Austin Salonen
+1  A: 

Try the following code:

ContextMenuStrip myMenuStrip = new ContextMenuStrip();
myMenuStrip.Show(myDataGrid , new Point(0 , 0));

and for a ContextMenu:

ContextMenu myMenu = new ContextMenu();
myMenu.Show(myDataGrid , new Point(e.X , e.Y));
dboarman