Venom Venom - 3 years ago 208
C# Question

ToolStripMenuItem location relative to parent

I am trying to extract the top left coordinate of the ToolStripMenuItem's location relative to that of its parent as it is rendered on screen. There is a Bounds property that has a Location member but it returns nonsensical (at least from my point of view) information regarding its actual location.

The "Click" event-handler looks something like this:

private void newShapeOption_Click(object sender, EventArgs e)
ToolStripMenuItem item = sender as ToolStripMenuItem;
Point point = item.Bounds.Location; // returns {X = 0, Y = 2} every time for some reason
// the rest is unimportant

The red dot (Paint skillz ;) on the image shows the exact position that I'd like to use (parent being the Form control named "Window 0" - it's and MDI app):


Answer Source

Context menus have no awareness of the physical location on the object where you clicked. That is the province of the OnClick event of the object itself. So if you want your Context Menu to have access to the click coordinates, you will have to hook the OnClick event of the object being clicked, capture the coordinates of the click in some form-global variables, and then use those values in your ContextMenu_Click event.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download