How dynamically, at the touch of a button, for example, add a line to System.Windows.Forms.TableLayoutPanel and add a button to this line.

UPDATE: Now the following code is used for adding:

this.tableLayoutPanel1.SuspendLayout(); this.SuspendLayout(); tableLayoutPanel1.BackColor = Color.Blue; for (int i = 0; i < 3; i++) { this.tableLayoutPanel1.RowCount = ++this.tableLayoutPanel1.RowCount; this.tableLayoutPanel1.Size = new System.Drawing.Size(200, this.tableLayoutPanel1.Size.Height + 100); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel1.Controls.Add(new Button(), 0, i); } this.tableLayoutPanel1.ResumeLayout(false); this.ResumeLayout(false); this.Refresh(); int j = tableLayoutPanel1.RowCount; 
  • one
    What is this line? Intrigued straight. - default locale
  • How to dynamically click a button, for example, add a line to System.Windows.Forms.TableLayoutPanel and add a button to this line. - Stanislav Petrov

2 answers 2

To add a row, simply increase the RowCount :

 tableLayoutPanel.RowCount++; 

You can add a component to a given cell using TableLayoutControlCollection.Add :

 //ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΈΠ»ΠΈ созданиС ΠΊΠ½ΠΎΠΏΠΊΠΈ Button button = new Button(); //какая-Ρ‚ΠΎ инициализация ΠΊΠ½ΠΎΠΏΠΊΠΈ //... //ДобавляСм ΠΊΠ½ΠΎΠΏΠΊΡƒ Π² ΠΏΠ΅Ρ€Π²ΡƒΡŽ (0) ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ послСднСй строки tableLayoutPanel.Controls.Add(button, 0, tableLayoutPanel.RowCount-1); 

PS In Visual Studio, you can build the desired interface, and then in the Π˜ΠΌΡΠ€ΠΎΡ€ΠΌΡ‹.designer.cs you Π˜ΠΌΡΠ€ΠΎΡ€ΠΌΡ‹.designer.cs see which designer generated the code.

  • I do it like this, but the button is not transferred to a new line. - Stanislav Petrov
  • @StanislavPetrov You do not add a button to a new line, but to line i (from 0 to 2): tableLayoutPanel1.Controls.Add(new Button(), 0, i); Replace i with tableLayoutPanel.RowCount-1 (last line number) - default locale
  • @StanislavPetrov Replace ResumeLayout(false) with ResumeLayout(true) - default locale

I add it like this.

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DynamicAddControls_CS { public partial class Form1:Form { public Form1() { InitializeComponent(); } private void button1_Click(Object sender,EventArgs e) { this.tableLayoutPanel1.SuspendLayout(); this.SuspendLayout(); tableLayoutPanel1.BackColor = Color.Blue; for (int i = 0; i < 3; i++) { this.tableLayoutPanel1.RowCount = ++this.tableLayoutPanel1.RowCount; this.tableLayoutPanel1.Size = new System.Drawing.Size(200, this.tableLayoutPanel1.Size.Height + 100); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel1.Controls.Add(new Button(), 0, i); } this.tableLayoutPanel1.ResumeLayout(false); this.ResumeLayout(false); this.Refresh(); int j = tableLayoutPanel1.RowCount; } } } 
  • New information on the issue must be added to the question itself. - default locale