开发者

C# and Locations on Forms

开发者 https://www.devze.com 2022-12-18 23:17 出处:网络
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

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.


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


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.


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));


you need to check for left and top properties

x + control.left
y + control.top
0

精彩评论

暂无评论...
验证码 换一张
取 消