4

I have a menustrip that I create using the forms designer. I then want to programmatically add a toolstrip toolbar UNDER the menustrip. The reason is that this toolbar is a set of tools that I add as a plugin to the main application. As such, when the plugin is loaded, I create the toolbar.

When I manually add the toolstrip to the form using the forms designer, the toolstrip is correctly positioned under the menustrip. However when I add it programmatically, it snaps to the topmost part of the form, above the menustrip. Here's the code I use to programmatically add the toolstrip:

stereoBar = new ToolStrip();
stereoBar.Anchor = (AnchorStyles)(AnchorStyles.Top | AnchorStyles.Left);
//y location is set to 22, the width of the menustrip
stereoBar.Location = new System.Drawing.Point(0, 22);
stereoBar.Dock = DockStyle.Top;
stereoBar.Name = "StereoToolbar";
stereoBar.Text = "Stereo Plugin Toolbar";
stereoBar.ShowItemToolTips = true;
stereoBar.GripMargin = new Padding(2);
Controls.Add(stereoBar);

Is there anything simple I am missing here?

Thanks in advance!

Carlo M.
  • 331
  • 1
  • 3
  • 12
  • Maybe you need add them to your menustrip indstead of Controls? – Hamlet Hakobyan Mar 12 '14 at 11:54
  • This is indeed pretty strange behaviour and also easy to reproduce, one thing i found really strange is that the generated designer code does not seem to include any `DockStyle` setting at all – DrCopyPaste Mar 12 '14 at 12:19
  • 1
    The order of the controls in the Controls collection matters. You'll need to append `stereoBar.BringToFront();` to get it to dock underneath the menu strip. – Hans Passant Mar 12 '14 at 14:40
  • @ HansPassant the BringToFront() works well, thanks for pointing that out! @DrCopyPaste I agree with you, I had a look at the generated designer code to try and figure out whether there was something different going on. The only thing I noted (which you pointed out below) is that in the designer code, the last thing to be added is the menustrip relative to all other controls being created automatically! – Carlo M. Mar 12 '14 at 16:43

1 Answers1

4

As described also in this answer, when docking controls in a form the order in which you add them to the form matters; i.e. when adding multiple controls that all dock to the top, the last control that gets added will be the topmost.

So, because you add your toolstrip programmatically and your menu strip via designer, the designer code gets executed first thus having the menu strip always at the bottom.

I think there are three ways out of the dilemma:

First approach

As Hans Passant pointed out, the easiest way to get things into the right order would be to simply call

stereoBar.BringToFront();

right after you added it to the forms' controls.

Second approach

To circumvent this you could also also add the menu strip programmatically and do this after you added the tool strip.

Third approach

Another way out may be to add another container to the form (Panel or groupbox for example) via designer that also docks to the top that simply just functions as a placeholder where you add your tool strip to (so you do not add to the form directly anymore)

DrCopyPaste
  • 4,023
  • 1
  • 22
  • 57
  • Thank you for your reply, you've provided good alternative solutions. The BringToFront() method solves the problem! – Carlo M. Mar 12 '14 at 16:41