在工具条键顿之后放置了一个工具条孔博,并由另一个是正确对准的刀具。我如何最好地设置工具combobox,以始终调整其长度以填充预制和foring toolstripbuttons之间的所有可用空间?

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

有帮助吗?

解决方案

没有自动布局选项。但是,您可以通过实现工具程序来轻松地做到这一点。这效果很好:

    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的自动化属性设置为false,否则它将无法正常工作。

其他提示

我成功地使用了以下内容:

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

上述代码不会遭受失去控制问题的困扰。

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

现在,Combobox将正确填充。相应设置列或行跨度。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top