C# – setting DockStyle.Fill for dynamic controls does not resize when i move the splitter

cdockdynamic-controlsnetwinforms

I have a split container on panel 1 I have added a groupbox, in that groupbox is a flowcontrol which has dynamic number of textboxes, i have set both the groupbox and flowcontrol to dockstyle to fill.
In code i have also set the textboxes to dock style to fill, but they wont resize when i move the splitter, while the parent flowcontrol does resize.

        Label labelInput = new Label();

        TextBox listBoxNewInput = new TextBox();


        listBoxNewInput.Name = ce.ToString();

        labelInput.AutoSize = true;
        labelInput.Font = new Font(labelInput.Font, FontStyle.Bold);



        listBoxNewInput.Multiline = true;
        // Add vertical scroll bars to the TextBox control.
        listBoxNewInput.ScrollBars = ScrollBars.Vertical;
        // Allow the RETURN key in the TextBox control.
        listBoxNewInput.AcceptsReturn = true;
        // Allow the TAB key to be entered in the TextBox control.
        listBoxNewInput.AcceptsTab = true;
        // Set WordWrap to true to allow text to wrap to the next line.
        listBoxNewInput.WordWrap = true;
        listBoxNewInput.Text = ts.ToString();
        //listBoxNewInput.Width = 150;
        listBoxNewInput.MinimumSize = new Size(200,150);
        listBoxNewInput.MaximumSize = new Size(1000, 150);

        listBoxNewInput.Dock = DockStyle.Fill;

        listBoxNewInput.TextChanged += new EventHandler(listBoxNewInput_TextChanged);
        //Add the newly created text box to the list of input text boxes
        inputTextBoxesList.Add(listBoxNewInput);

        //Add the labels and text box to the form

        flowLayoutPanel1.Controls.Add(labelInput);
        flowLayoutPanel1.Controls.Add(listBoxNewInput);

if i try to put controls directly in to spliter panel 1 only the first two controls appear, which do resize when i move the splitter

splitContainer1.Panel1.Controls.Add(labelInput); splitContainer1.Panel1.Controls.Add(listBoxNewInput);

->if the controls when i put them in flow control resize, when i move the splitter that would be good
OR
->All controls appear when i put them directly into the splitter panel 1

Best Answer

Based on your comments and what I think you are trying to accomplish, I think you need to replace the FlowLayoutPanel with a TableLayoutPanel because it sounds like you are just stacking one TextBox below another.

Create a TableLayoutPanel with 1 column and 1 row.

Here is a working example:

tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 150));
for (int i = 0; i < 4; i++) {
  AddTextBox("TextBox #" + i.ToString());
}

private void AddTextBox(string info) {
  TextBox tx = new TextBox();
  tx.Multiline = true;
  tx.Text = info;
  tx.ScrollBars = ScrollBars.Vertical;
  tx.WordWrap = true;
  tx.Height = 150;
  tx.Anchor  = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
  tableLayoutPanel1.Controls.Add(tx);
}

Instead of docking, I set the height of the TextBox and then I set the Anchors so that when the SplitPanel resizes, the TextBoxes resize appropriately.