4

我想以编程方式用项目填充 ToolStrip。toolStrip1.LayoutStyle 必须是ToolStripLayoutStyle.Table。但是我无法更改项目的宽度,即使我这样做了,宽度也会恢复为默认值。

ToolStripLabel tslCheck = new ToolStripLabel("First Text"),
                           tslSumm = new ToolStripLabel("second Text:"),
                           tslError = new ToolStripLabel("third text:"),
                           tslAccount = new ToolStripLabel("fourth text"),
                           tslProvider = new ToolStripLabel("fifth text");
ToolStripTextBox tbCheck = new ToolStripTextBox(),
                           tbAccount = new ToolStripTextBox(),
                           tbSumm = new ToolStripTextBox(),
                           tbError = new ToolStripTextBox();
TableLayoutSettings tsSettings;

ToolStripComboBox cbProvider = new ToolStripComboBox();       

protected void toolStrip1_Construct()
{
    toolStrip1.LayoutStyle = ToolStripLayoutStyle.Table;            
    tsSettings = toolStrip1.LayoutSettings as TableLayoutSettings;
    tsSettings.RowCount = 2; 
    tsSettings.ColumnCount = 6;

    cbProvider.DropDownStyle = ComboBoxStyle.DropDownList;

    ///adding controls
    toolStrip1.Items.AddRange(new ToolStripItem[]
    {
        tslCheck,
        tbCheck,

        tslAccount,
        tbAccount,

        tslProvider,
        cbProvider,

        //second row
        tslSumm,
        tbSumm,

        tslError,
        tbError
    });

    tbAccount.Width = 1000; //Width is still remains 100
    cbProvider.Width = 500; //Width is still remains 121
}

但是,如果我在 VisualDesigner 中进行,一切正常。

4

2 回答 2

3

您必须将AutoSize属性设置为 false:

ToolStripComboBox cbProvider = new ToolStripComboBox() { AutoSize = false };
于 2012-05-31T13:36:40.113 回答
0

实际上对我有用的是设置 Size 属性而不是 Width 属性:

cbProvider.Size = new Size(40, 20);

高度被忽略,但宽度被正确设置

于 2019-03-19T20:18:35.607 回答