I have a Windows Forms ToolStripPanel that I dynamically add new ToolStrips to it. Each ToolStrip has ToolStripItems that I dynamically add to it (mostly ToolStripButtons).
I would like to have all the ToolStrips fit on one row as long as you can see all the ToolStripItems in each ToolStrip. If adding a new ToolStripItem to a ToolStrip causes overflow to occur, I'd like the ToolStrip at the end of the row to jump down to a new row. The only time I'd like to see the overflow button is if the ToolStrip has so many items that they can't fit on the width of the form.
I want this to be the default behavior unless a user manually moves a ToolStrip. If the user moves the ToolStrip, then I want the existing overflow (e.g. overflow button with drop down choices) behavior to occur. I assume the user will be OK with this default Winforms overflow behavior if they position the toolbar themselves.
If a new dynamic toolbar is added after the user repositions things, I'd like the new toolbar to be added to the right of the last ToolStrip in the bottom row. This toolbar should have the updated overflow behavior (e.g. use a new row if you can't fit) until the user manually repositions it.
I've done some research without great success:
- This seems like it should be a simple matter of changing a ToolStripLayoutStyle, but this doesn't seem to be the case. (At best, new rows are created when the overflow button is showing on all ToolStrips in a given row).
- The MSDN article "How to: Manage ToolStrip Overflow in Windows Forms" seems to only cover if/when the overflow button occurs.
- There is a LayoutCompleted event on the ToolStrip that seems possible to use to tweak things, it seems like there should be an easier way.
- Creating a custom LayoutEngine seems like an overkill for this since the built-in one is very close to what I want to do.
Am I missing an easy solution?
UPDATE: Still looking for a good solution. It seems the LayoutCompleted route is possible. The problem with it though is that it is called multiple times. One time it says the ToolStripItem is in the overflow area and immediately after that is in the Main area, so it doesn't seem safe to just check one time if it's in overflow.
1) Add the items to the toolstrip
2) Compare the width of the toolstrip to the amount of space left on the last row. The ToolStripPanel has a Rows[] property that will tell you what the last row is and what toolstrips are on that row. Add up their widths, and subtract them from the width of the panel.
3) Add the new toolstrip to the panel using the Join method, specifying either the last row (if it fits) or one row higher (if it doesn't).
In the end, I don't think there is a good easy solution. This thread makes it clear that ToolStrips weren't really designed for dynamic scenarios as a priority.
One unique approach is to set the ToolStrip's MinimumSize to be the size of all the buttons. This will start to work with overflow like I describe, but even this has problems with edge cases.
The best approach I've found is to do everything possible to get a good feel for what the size of the toolbar should be in advance and then position it on the ToolStripPanel using the Join method. If you make a reasonable guess ahead of time, then it's probably ok if you add more buttons later as they'll end up in the overflow area.
来源:https://stackoverflow.com/questions/604713/change-toolstrippanel-overflow-behavior-for-dynamically-added-toolstripitems