6

ToolStripComboBox 放置在 ToolStripButton 之后,然后是另一个右对齐的。如何最好地设置 ToolStripComboBox 以始终调整其长度以填充前面和后面的 ToolStripButtons 之间的所有可用空间?

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

4

3 回答 3

6

我使用以下方法取得了巨大成功:

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);
}

上面的代码不存在消失的控制问题。

于 2010-06-17T16:19:12.610 回答
2

没有自动布局选项。但是您可以通过实现 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,否则它将不起作用。

于 2010-05-04T21:37:34.363 回答
1
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);

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

于 2015-08-13T20:25:33.370 回答