动态地向tablelayout添加面板 [英] Adding panels to tablelayout dynamically
本文介绍了动态地向tablelayout添加面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一组面板,每个面板都有一组动态添加的控件(面板内的面板和控件)。我想一次一行地将这些面板添加到tablelayout,并希望tablelayout自动增长并相应地将控件
推到它下面。
有人可以给我一个exxample代码
由于
解决方案
<预风格= "">&NBSP;?&NBSP;&NBSP;&NBSP ; public partial class Form1 : 表格
; {
public Form1()
{
InitializeComponent();
indexStart = this .tableLayoutPanel1 .RowStyles.Count;
}
private static int indexStart;
private void button1_Click( object sender, EventArgs e )
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; System.Windows.Forms。 Panel panel1;
System.Windows.Forms。 Label &NBSP; label1的;
panel1 = new System.Windows.Forms。 Panel ();
label1 = new System.Windows.Forms。 Label ();
//
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的 //&NBSP; PANEL1 跨度>
&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的 //&NBSP; 跨度>
&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; panel1.Controls.Add(label1的);
panel1.Location = new System.Drawing。 Point (3, 3);
panel1.Name = " panel1" 跨度>;
panel1.Size = new System.Drawing。尺寸(433, 258);
panel1.TabIndex = 0;
//
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的 //&NBSP; label1的跨度>
&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的 //&NBSP; 跨度>
&NBSP;&NBSP;&NBSP;&NBSP ; label1.AutoSize = true ;
label1.Location = new System.Drawing。 Point (184, 104);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; label1.Name&NBSP; =&NBSP;的" label1的" 跨度>;
label1.Size = new System.Drawing。尺寸(35, 13);
label1.TabIndex = 0;
label1.Text = " label1" 跨度>;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的此跨度> .tableLayoutPanel1。 Controls.Add(panel1, 0, this .tableLayoutPanel1.RowCount ++);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的此跨度> .tableLayoutPanel1。 RowStyles.Add( new RowStyle ( SizeType .Percent, 100F / 此 .tableLayoutPanel1.RowCount));
for ( int i = 0; i < this .tableLayoutPanel1.RowStyles.Count- 1;&NBSP;我++)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; this .tableLayoutPanel1.RowStyles [i] .SizeType = < span style ="color:#2b91af"> SizeType .Percent;
this .tableLayoutPanel1.RowStyles [i] .Height = 100F / this .tableLayoutPanel1.RowCount;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
&NBSP;&NBSP;&NBSP;&NBSP;}
对于小组的和控制的大小和位置号码,可以锻炼一个表达式来计算它们基座上的tabeLayoutPanel1.Size值和行数。
Hi,
I have a set of panels and each panel has a set of controls added dynamically (both panels and controls inside the panels). I want to add these panels one row at a time to the tablelayout and want the tablelayout to grow automatically and push the controls below it accordingly.
Can someone please give me an exxample code?
Thanks
解决方案
public partial class Form1 : Form { public Form1() { InitializeComponent(); indexStart = this.tableLayoutPanel1.RowStyles.Count; } private static int indexStart; private void button1_Click(object sender, EventArgs e) { System.Windows.Forms.Panel panel1; System.Windows.Forms.Label label1; panel1 = new System.Windows.Forms.Panel(); label1 = new System.Windows.Forms.Label(); // // panel1 // panel1.Controls.Add(label1); panel1.Location = new System.Drawing.Point(3, 3); panel1.Name = "panel1"; panel1.Size = new System.Drawing.Size(433, 258); panel1.TabIndex = 0; // // label1 // label1.AutoSize = true; label1.Location = new System.Drawing.Point(184, 104); label1.Name = "label1"; label1.Size = new System.Drawing.Size(35, 13); label1.TabIndex = 0; label1.Text = "label1"; this.tableLayoutPanel1.Controls.Add(panel1, 0, this.tableLayoutPanel1.RowCount++); this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 100F / this.tableLayoutPanel1.RowCount)); for (int i = 0; i < this.tableLayoutPanel1.RowStyles.Count-1; i++) { this.tableLayoutPanel1.RowStyles[i].SizeType = SizeType.Percent; this.tableLayoutPanel1.RowStyles[i].Height = 100F / this.tableLayoutPanel1.RowCount; } } }For the Panel's and control's size and location numbers, you can workout a expression to calculate them base on the tabeLayoutPanel1.Size value and the row count.
这篇关于动态地向tablelayout添加面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文