1

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Venom
  • 1,107
  • 4
  • 21
  • 46
  • Is that really a ToolStrip? When I drag a toolstrip onto my Winform, it automatically docks itself at the top of the form, which corresponds roughly to X=0, Y=2. – Robert Harvey Nov 30 '14 at 17:54
  • @RobertHarvey It is docked at the top but this is a ToolStripMenuItem (a child control I believe). Bound.Location obviously returns the same location as the parent's which I guess makes sense since this is the only child and consequently the first, also. Shame that there is no way to get the location of the actual rendered graphical object. – Venom Nov 30 '14 at 17:58
  • What is the location of the actual rendered graphical object? Logically, the first menu item in a docked toolstrip object should be located at the upper left hand corner of the toolstrip, give or take a few pixels. – Robert Harvey Nov 30 '14 at 17:59
  • @RobertHarvey It collapsed the child (ToolStripMenuItem) according to its container (ContextMenuStrip in my case). Was that a rhetorical question? I'm sorry if I didn't understand. The only thing I can think of is to remember the cursor's location when the user right-clicks and use that. – Venom Nov 30 '14 at 18:03
  • Why do you have a ContextMenuStrip in the middle of the form? Oh, you mean a Context Menu popup? There's no notion of where that popup appears; if you want to take action on some object you've right-clicked on, you have to obtain that object, not a pixel location. In your case, it's most likely "New Shape." – Robert Harvey Nov 30 '14 at 18:03
  • @RobertHarvey It looks like this in Visual Studio's Designer - http://i.imgur.com/zB5h5xt.png The ContextMenuStrip is automatically docked at the top. – Venom Nov 30 '14 at 18:05
  • So you're trying to drop a new shape at a specific location on the screen, is that it? – Robert Harvey Nov 30 '14 at 18:06
  • @RobertHarvey Exactly that, right where the user right-clicked on the form. – Venom Nov 30 '14 at 18:06
  • 1
    Normally the way that's done is with a toolbar. You click on the desired shape on the toolbar, then click on the surface and capture surface.OnClick (which gets you the coordinates). A right-click on the surface (Context menu or no) should work the same way; you get the coordinates by capturing the OnClick method of the surface. Presumably, you store those coordinates in some form-global variables for use by the context menu, so that it knows where to put the new shape. – Robert Harvey Nov 30 '14 at 18:08
  • Agreed. Trap the MouseDown event of your Form and check for a Right Click. Store those coords in variable that can be accessed by your "new shape" routine. – Idle_Mind Nov 30 '14 at 18:47
  • @Idle_Mind I've used the MouseUp event but it is the same thing more or less in this case. One of you two ought to post answer if you want. Maybe you'd prefer if I simply deleted the question? – Venom Nov 30 '14 at 18:56
  • @RobertHarvey should post as an answer in my opinion. – Idle_Mind Nov 30 '14 at 19:00
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Nov 30 '14 at 20:02

2 Answers2

2

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
0

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