Can't resize ToolStripItem programmatically in ToolStrip with ToolStripLayoutStyle.Table

StackOverflow https://stackoverflow.com/questions/10832876

  •  11-06-2021
  •  | 
  •  

Question

I want to filling the ToolStrip with items programmatically. The toolStrip1.LayoutStyle must be ToolStripLayoutStyle.Table. But i can't change Width of item, even i've do that, the Width restores to default.

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
}

But, if i do it in VisualDesigner, everything works fine.

Was it helpful?

Solution

You have to turn the AutoSize property to false:

ToolStripComboBox cbProvider = new ToolStripComboBox() { AutoSize = false };

OTHER TIPS

What actually worked for me was to set the Size property instead of the Width property:

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

Height is ignored but Width gets set properly

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top