2017-03-16 58 views
0

我想在单击按钮时动态地将控件添加到面板。但我想组织职位。例如,我想要两个宽度相等的文本框以相等的面板空间宽度。看下面的图片。在.NET窗口表格中向TableLayoutPanel添加动态控件

enter image description here

正如你可以在上面的图片,单击该按钮时,控件将被添加看到。但是我在使用TableLayoutPanel时遇到问题。看到我的代码如下。

private void btnAddOption_Click(object sender, EventArgs e) 
     { 
      TextBox tb1 = new TextBox(); 
      tb1.Text = "Cell 1"; 
      TextBox tb2 = new TextBox(); 
      tb2.Text = "Cell 2"; 


      TableLayoutPanel rowLayout = new TableLayoutPanel(); 
      rowLayout.ColumnCount = 2; 
      rowLayout.RowCount = 1; 

      //want to add tb1 to cell 1 and tb2 to cell 2 of TableLayoutPanel   

      panelFoodOptions.Controls.Add(rowLayout); 

     } 

正如您在我的代码中看到的,我评论了我想要做的事情。这些是我的问题。

我想这

rowLayout.Controls.Add(tb1); 
rowLayout.Controls.Add(tb2); 

所以上述方法不起作用。所以我尝试了一种方法来获得布局单元格。但我有问题。看下面的图片。

enter image description here

正如你在截图中看到的,我必须要通过子控件来获取细胞。但我甚至没有添加控制单元。我想将控制权添加到获取其各自位置的单元格中。我如何将控制添加到我想要的单元格?

+0

[?如何创建使用Windows窗体幻方(http://stackoverflow.com/questions/ 33968993/how-to-create-a-magic-square-using-windows-forms) –

回答

1

你只需要使用Controls.Add方法,并指定列和行的控制:

rowLayout.Controls.Add(tb1, 0, 0); 
rowLayout.Controls.Add(tb2, 0, 1); 
+0

非常感谢。有效。 :) –

+0

@WayYanHein您的欢迎,下次记得检查你正在使用的方法超负荷:) –

相关问题