我正在开发一个应用程序,在该应用程序中,控件列表被添加到表单中,并且在按钮上单击(编辑)用户可以在运行时拖放控件。我想安排用户在按钮点击后按照列和行格式的表格拖放控件,为此我动态创建了tablelayout。但是表格布局似乎是在控件本身内部创建的,我无法从工具箱中添加tablelayout,因为它阻止/隐藏了在运行时添加的控件。我使用下面的代码来创建tablelayout。
void fnEngageControlMover()
{
//Control con;
foreach (Control llbl in pnlShortcuts.Controls)
{
libs.ControlMover.createtable(llbl);
if (llbl.Visible)
{
libs.ControlMover.Init(llbl, pnlShortcuts);
if (llbl.Controls.Count > 0)
{
foreach (Control lctx in llbl.Controls)
{
if (lctx.Name.Length > 8 && lctx.Name.Substring(0, 8) == "lpnlBtns")
{
llbl.Width = llbl.Width + lctx.Width;
lctx.Visible = true;
}
}
}
}
}
}
public static void createtable(Control panel)
{
TableLayoutPanel dynamicTableLayoutPanel = new TableLayoutPanel();
dynamicTableLayoutPanel.Location = new System.Drawing.Point(26, 12);
dynamicTableLayoutPanel.Name = "TableLayoutPanel1";
dynamicTableLayoutPanel.Size = new System.Drawing.Size(228, 200);
dynamicTableLayoutPanel.BackColor = Color.LightBlue;
// Add rows and columns
dynamicTableLayoutPanel.ColumnCount = 3;
dynamicTableLayoutPanel.RowCount = 5;
dynamicTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
dynamicTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
dynamicTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 40F));
dynamicTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 52F));
dynamicTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 44F));
dynamicTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 44F));
dynamicTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 38F));
dynamicTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 8F));
panel.Controls.Add(dynamicTableLayoutPanel);
}
}
}