0

I'm not new to programming but somewhat new to .Net and trying out some menu related things in .Net6. My objective (C# in WinForms) is a menu with some items docked Left and some docked Right, all accomplished at runtime. A crude representation:

| menuitem1 | menuitem2 --- (space between menuitems) --- menuitem3 | menuitem4 |

Creating ToolStripItem objects is fine but trying to set tsItem.Dock = DockStyle.Right seems meaningless. It has no affect. Same with trying DockStyle.Fill.

Also tried placing 2 ToolStrips on ToolStripPanel and Join them by specifying Row=0. The 2 ToolStrips end up on the left on 2 different rows, not on the same row to look like one menu.

I must be missing something, probably something simple, an oversight. But I have spent 2 days without understanding. To do this with Panel and non-ToolStripItem classes is simple. What have I missed? Thank you in advance for assistance.

ankor
  • 1
  • 3
  • You can build your own Component which implements ToolStripItem or an already existing derived class, decide its availability with MenuStrip, ToolStrip, StatusStrip etc. decorating the class with the desired [ToolStripItemDesignerAvailability](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.design.toolstripitemdesigneravailability) and make it behave as you please. An example here: [ToolStripLabel not updated with application settings using PropertyBinding](https://stackoverflow.com/a/65580860/7444103) -- It's not clear if you have a ToolStrip or a MenuStrip there. – Jimi Mar 02 '22 at 01:41
  • If you just need to *add space* between ToolStrip Components, you could simply add a ToolStripLabel, without any text, set `AutoSize = false` and use it to *space* other ToolStripItems. Yes, it's hacky. – Jimi Mar 02 '22 at 01:48
  • You can also handle the Left / Right `Margin` to add space between elements in a ToolStrip or MenuStrip. – Jimi Mar 02 '22 at 08:57
  • Thanks for the speedy replies. The inquiry wasn't specific to ToolStrip or any specific descendant. It was about general behavior for all of them (hence no code). I'm rather surprised and disappointed this warrants a hack "solution". Really thought I just hadn't discovered the right piece of documentation. But I wondered if the menu overflow functionality interfered with docking support...perhaps. The suggested hacks are interesting though. I'll need to experiment and see what might be suitable. This is much more a fun project to familiarize with and further learn .NET6. Thanks again! – ankor Mar 02 '22 at 20:03
  • Not *suggested hacks*, just one, the ToolStripLabel as *spacer*. That's just a simplification, but can be used sometimes. What's in the first comment is the proper method, used to generate custom behavior, something more complex that just the default alignment of ToolStrip / MenuStrip etc. items. The last one - setting the `Margin` Property - is a pretty standard method to space UI Items. – Jimi Mar 02 '22 at 20:29
  • Had to try out the suggestions...the blank Label and Left/Right Margin manipulation. And to my surprise both approaches actually look nice and it doesn't feel so much of a hack as I thought it might. The Label might have a slight edge over Margin. ;) Perhaps a Form.Resize event could work it dynamically. Very cool. – ankor Mar 02 '22 at 20:32
  • I'll need to set aside some time to experiment with the class descendant approach. Thanks again. – ankor Mar 02 '22 at 20:36

1 Answers1

0

Just stumbled across the ToolStripItem.Alignment property. The goal was to be able to Dock ToolStripItem objects on the right side of a horizontal menu (i.e. ToolStrip, MenuStrip). The Dock property doesn't do this with ToolStripItem. Instead it uses the ToolStrip.Alignment property (a ToolStripItemAlignment enum) which has 2 values...Left (default) and Right. By setting Alignment to Right causes the ToolStripItem to align to the right side of the menu. Perfect! So it seems the ToolStripItem.Alignment property is the answer to docking menu items to the right side of the menu.

ankor
  • 1
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 03 '22 at 06:09