Reputation: 320
I have placed all the controls in one panel.During the run time i would like to add the control to table layout panel. To do that i have written following code.
void arrangeTableLayout()
{
int rowcount = 1;
tblPanel.ColumnCount=2;
tblPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
tblPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
for (int i = 0; i < panel.Controls.Count; i++)
{
if (panel.Controls[i].Visible)
{
tblPanel.Controls.Add(panel.Controls[i], 0, rowcount);
tblPanel.Controls.Add(panel.Controls[i + 1], 1, rowcount);
tblPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40F));
i++;
rowcount++;
}
}
}
in the control array the controls are there as required to me. But the above code adding only labels in one column.
Can any one tell me how to add the windows form controls dynamically to the table layout panel.
Upvotes: 0
Views: 11975
Reputation: 320
Its worked for me.
void arrangeTableLayout()
{
int rowcount = 1;
tblPanel.ColumnCount=2;
tblPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
tblPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
for (int i = 0; i < panel.Controls.Count; i++)
{
if (panel.Controls[i].Visible)
{
var c1 = panel.Controls[i];
var c2 = GetNextControl(panel.Controls[i], true);
panel.Controls.Remove(c1);
i--;
panel.Controls.Remove(c2);
tblPanel.Controls.Add(c1, 0, rowcount);
tblPanel.Controls.Add(c2, 1, rowcount);
tblPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 40F));
rowcount++;
}
}
}
Upvotes: 1
Reputation: 1772
You can use this code to do that.
tblPanel.Controls.Add(new Label() { Text = "Type:", Anchor = AnchorStyles.Left, AutoSize = true }, 0, 0);
tblPanel.Controls.Add(new ComboBox() { Dock = DockStyle.Fill }, 0, 1);
you don't need to define number of rows and columns, they will be added automatically.
Combobox use as a example. Replace it with your controls
Upvotes: 0