ToolStripMenuItem location relative to parent

2.7k views Asked by At

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

Example

2

There are 2 answers

0
Robert Harvey On BEST ANSWER

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.

0
Aliakbar On

You can use item width, such as this example: I want to put label1 after menu items

 int w = 0;
 for (int i = 0; i < menuStrip1.Items.Count; i++)
 {
     w += menuStrip1.Items[i].Width;
 }
 label1.Width = (int)(this.Width - w);
 label1.Location = new Point(w, label1.Location.Y);