创建嵌套TableLayoutPanel时出现问题

时间:2013-07-02 11:19:56

标签: c# winforms tablelayoutpanel

我尝试实现Nested TableLayoutPanel。我尝试在父TableLayoutPanel中动态创建/加载子TableLayoutPanel。 为此,我采用父TableLayoutPanel并从visual studio工具箱中绘制它。 一个DropDownList用于动态创建子TableLayoutPanel我将一些值分配给dropdownlist,例如2 * 2,2 * 3,3 * 3,4 * 4,当选择的索引更改为fire时,绘制子TableLayoutPanel。 我的代码在

之下
private void cmbRowsColumns_SelectedIndexChanged(object sender, EventArgs e)
{   
  var selectedPair = new KeyValuePair<string, string>();
  selectedPair = (KeyValuePair<string, string>)cmbRowsColumns.SelectedItem;
  string[] rowcolumn = selectedPair.Value.Split('*');
  string strRowCount = rowcolumn[0];
  int rowCount = Convert.ToInt32(strRowCount);
  string strColumnCount = rowcolumn[1];
  int columnCount = Convert.ToInt32(strColumnCount);
  DynamicallyGenerateColumn(rowCount, columnCount);
}


private void DynamicallyGenerateColumn(int rowCount, int columnCount)
{
  parentTableLayoutPanel.Controls.Clear();
  parentTableLayoutPanel.ColumnStyles.Clear();
  parentTableLayoutPanel.RowStyles.Clear();
  parentTableLayoutPanel.ColumnCount = columnCount;
  parentTableLayoutPanel.RowCount = rowCount;
  for (int i = 0; i < columnCount; i++)
    {
      parentTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
      for (int j = 0; j < rowCount; j++)
        {
        if (i == 0)
         {
          parentTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
         }
          TableLayoutPanel objTableLayoutPanel = new  TableLayoutPanel();                  
          parentTableLayoutPanel.Controls.Add(objTableLayoutPanel, i, j);
       }
     }
 }

enter image description here 但实际问题是当我创建子TableLayoutPanel格式不正确时

1 个答案:

答案 0 :(得分:0)

我想你想要填充每个子面板所以你需要添加objTableLayoutPanel.Dock=DockStyle.Fill;

TableLayoutPanel objTableLayoutPanel = new  TableLayoutPanel(); 
objTableLayoutPanel.Dock = DockStyle.Fill;
parentTableLayoutPanel.Controls.Add(objTableLayoutPanel, i, j);