0

我正在尝试实现嵌套 TableLayoutPanel。我尝试在父 TableLayoutPanel 中动态创建/加载子 TableLayoutPanel。为此,我采用父 TableLayoutPanel 并从 Visual Studio 工具箱中绘制它。一个动态创建Child TableLayoutPanel的下拉列表我为下拉列表分配了一些值,例如2*2,2*3,3*3,4*4当所选的索引更改为FIRE时,请绘制Child 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);
       }
     }
 }

在此处输入图像描述 但实际上问题是当我创建子 TableLayoutPanel 格式不正确

4

1 回答 1

0

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

TableLayoutPanel objTableLayoutPanel = new  TableLayoutPanel(); 
objTableLayoutPanel.Dock = DockStyle.Fill;
parentTableLayoutPanel.Controls.Add(objTableLayoutPanel, i, j);
于 2013-07-02T12:19:15.773 回答