How to open form at toolstrip button position

3.9k views Asked by At

I have toolstrip menu with some buttons, menu is located in toolstrip container. What I am trying to accomplish is to open new form exactly at specific toolstripbutton location... This is my code. It works unless I move toolstrip menu to bottom or right side of toolstrip container...

private System.Windows.Forms.ToolStripButton rbRunMacro;
private System.Windows.Forms.ToolStrip tsMacroRecorder;

        private void rbRunMacro_Click(object sender, EventArgs e)
    {
        Rectangle rect = this.rbRunMacro.Bounds;
        Point location = PointToScreen ( new Point(this.tsMacroRecorder.Location.X + rect.X, this.tsMacroRecorder.Location.Y + rect.Y));
        MacroListForm form = new MacroListForm();
        form.StartPosition = FormStartPosition.Manual;
        form.Location = location ;
        form.Show();
    }
1

There are 1 answers

1
osvein On BEST ANSWER

You should get the location of rbRunMacro relative to the entire screen (see link).

form.Location = this.tsMacroRecorder.PointToScreen(rbRunMacro.Bounds.Location);

The reason we use ToolStrip.PointToScreen is that ToolStripButton does not offer the PointToScreen method. Therefore, we have to use ToolStripButton.Location to get the location of rbRunMacro relative to its parent control (see link).