Cannot Resize ToolStripItem Programmatically in ToolStrip Using ToolStripLayoutStyle.Table

I want to populate ToolStrip with elements programmatically. Tool toolStrip1.LayoutStyle must be ToolStripLayoutStyle.Table . But I can’t change the Width of the element, even I do it, Width restores the default value.

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 this in VisualDesigner, everything works fine.

+4
source share
1 answer

You need to turn the AutoSize property to false:

 ToolStripComboBox cbProvider = new ToolStripComboBox() { AutoSize = false }; 
+1
source

Source: https://habr.com/ru/post/1415345/


All Articles