如何制作 ToolStripComboBox 来填充 ToolStrip 上的所有可用空间?

发布于 2024-08-31 16:13:59 字数 213 浏览 9 评论 0原文

ToolStripComboBox 放置在 ToolStripButton 后面,后面是另一个右对齐的工具条组合框。如何最好地将 ToolStripComboBox 设置为始终调整其长度以填充前后 ToolStripButton 之间的所有可用空间?

过去,我曾经处理父级调整大小事件,根据相邻元素坐标计算要设置的新长度并设置新大小。但现在,当我开发一个新的应用程序时,我想知道是否有更好的方法。

A ToolStripComboBox is placed after a ToolStripButton and is folowed by another one, which is right-aligned. How do I best set up the ToolStripComboBox to always adjust its length to fill all the space available between the preceeding and the folowing ToolStripButtons?

In past I used to handle a parent resize event, calculate the new length to set based on neighboring elements coordinates and setting the new size. But now, as I am developing a new application, I wonder if there is no better way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下壹個目標 2024-09-07 16:13:59

我使用以下代码取得了巨大成功:

private void toolStrip1_Layout(System.Object sender, System.Windows.Forms.LayoutEventArgs e)
{
    int width = toolStrip1.DisplayRectangle.Width;

    foreach (ToolStripItem tsi in toolStrip1.Items) {
        if (!(tsi == toolStripComboBox1)) {
            width -= tsi.Width;
            width -= tsi.Margin.Horizontal;
        }
    }

    toolStripComboBox1.Width = Math.Max(0, width - toolStripComboBox1.Margin.Horizontal);
}

上面的代码不会遇到控制消失问题。

I use the following with great success:

private void toolStrip1_Layout(System.Object sender, System.Windows.Forms.LayoutEventArgs e)
{
    int width = toolStrip1.DisplayRectangle.Width;

    foreach (ToolStripItem tsi in toolStrip1.Items) {
        if (!(tsi == toolStripComboBox1)) {
            width -= tsi.Width;
            width -= tsi.Margin.Horizontal;
        }
    }

    toolStripComboBox1.Width = Math.Max(0, width - toolStripComboBox1.Margin.Horizontal);
}

The above code does not suffer from the disapearing control problem.

当梦初醒 2024-09-07 16:13:59

没有自动布局选项。但是您可以通过实现 ToolStrip.Resize 事件轻松地做到这一点。这很有效:

    private void toolStrip1_Resize(object sender, EventArgs e) {
        toolStripComboBox1.Width = toolStripComboBox2.Bounds.Left - toolStripButton1.Bounds.Right - 4;
    }
    protected override void OnLoad(EventArgs e) {
        toolStrip1_Resize(this, e);
    }

请务必将 TSCB 的 AutoResize 属性设置为 False,否则它将不起作用。

There's no automatic layout option for this. But you can easily do it by implementing the ToolStrip.Resize event. This worked well:

    private void toolStrip1_Resize(object sender, EventArgs e) {
        toolStripComboBox1.Width = toolStripComboBox2.Bounds.Left - toolStripButton1.Bounds.Right - 4;
    }
    protected override void OnLoad(EventArgs e) {
        toolStrip1_Resize(this, e);
    }

Be sure to set the TSCB's AutoResize property to False or it won't work.

勿挽旧人 2024-09-07 16:13:59
ToolStrip ts = new ToolStrip();

ToolStripComboBox comboBox = new TooLStripComboBox();
comboBox.Dock = DockStyle.Fill;

ts.LayoutStyle = ToolStripLayoutStyle.Table;
((TableLayoutSettings)ts.LayoutSettings).ColumnCount = 1;
((TableLayoutSettings)ts.LayoutSettings).RowCount = 1;
((TableLayoutSettings)ts.LayoutSettings).SetColumnSpan(comboBox,1);

ts.Items.Add(comboBox);

现在组合框将正确停靠填充。相应地设置列或行跨度。

ToolStrip ts = new ToolStrip();

ToolStripComboBox comboBox = new TooLStripComboBox();
comboBox.Dock = DockStyle.Fill;

ts.LayoutStyle = ToolStripLayoutStyle.Table;
((TableLayoutSettings)ts.LayoutSettings).ColumnCount = 1;
((TableLayoutSettings)ts.LayoutSettings).RowCount = 1;
((TableLayoutSettings)ts.LayoutSettings).SetColumnSpan(comboBox,1);

ts.Items.Add(comboBox);

Now the combobox will dock fill correctly. Set Column or Row span accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文