Change ToolStripPanel overflow behavior for dynamically added ToolStripItems

一世执手 提交于 2019-12-06 05:10:58

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!