0

I want to show a ContextMenuStrip at the location of a ToolStripStatusLabel in a StatusStrip. Ordinary controls have PointToScreen / PointToClient / etc, but as ToolStripStatusLabel is derived from Component it does not.

Any help would be appreciated.

4 Answers4

3

A very, very late answer, just because I happened to struggle with the same issue and googled up this question. What I found as the best solution adds one nice twist to the answers so far. Here it is:

    void toolStripItem_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            var label = (ToolStripItem)sender;
            this.contextMenuStrip1.Show(this.mainStatusStrip, label.Bounds.X + e.X, label.Bounds.Y + e.Y);
        } 
    }

Adding the mouse coordinates relative to the control (e.X, e.Y) to the bounds coordinates makes the menu appear at exactly the right position. Omitting this shows the menu at the top left corner of the ToolStripItem. For the record.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
0

Can't you just do something like this:

int x = label.Bounds.Location.X + statusStrip.Location.X;
int y = label.Bounds.Location.Y + statusStrip.Location.Y;
menu.Show(this, x, y);
drs9222
  • 4,448
  • 3
  • 33
  • 41
0

Check out the Bounds property of the ToolStripStatusLabel. Use it like this to do what you need to do:

contextMenuStrip1.Show(statusStrip1, toolStripStatusLabel2.Bounds.X, toolStripStatusLabel2.Bounds.Y);            
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
0

There are a couple of steps involved in getting the proper location of the mouse on the form when it's clicked on a ToolStripStatusLabel. As you mention the ToolStripStatusLabel does not have PointToClient or PointToScreen methods, but the parent StatusStrip control does.

Try:

private void toolStripStatusLabel_MouseDown(object sender, MouseEventArgs e)
{
    Point p = e.Location;
    p.Offset(toolStripStatusLabel.Bounds.Location);
    myContextMenu.Show(StatusStrip.PointToScreen(p));
}
jac
  • 9,666
  • 2
  • 34
  • 63