2011-07-25 90 views
-3

我需要動態地添加一些控件,作爲我的數組中的元素數量,我如何可以把它們放在一個動態的順序,例如我有這個控制而且我必須加倍他們,這個代碼我必須加倍插入動態控制在win c#

this.label1 = new System.Windows.Forms.Label(); 
     this.textBox1 = new System.Windows.Forms.TextBox(); 
     this.label2 = new System.Windows.Forms.Label(); 
     this.label3 = new System.Windows.Forms.Label(); 
     this.label4 = new System.Windows.Forms.Label(); 
     this.label5 = new System.Windows.Forms.Label(); 
     this.textBox2 = new System.Windows.Forms.TextBox(); 
     this.textBox3 = new System.Windows.Forms.TextBox(); 
     this.SuspendLayout(); 
     // 
     // label1 
     // 
     this.label1.AutoSize = true; 
     this.label1.Location = new System.Drawing.Point(685, 80); 
     this.label1.Name = "label1"; 
     this.label1.Size = new System.Drawing.Size(23, 13); 
     this.label1.TabIndex = 0; 
     this.label1.Text = "name"; 
     // 
     // textBox1 
     // 
     this.textBox1.Location = new System.Drawing.Point(507, 77); 
     this.textBox1.Name = "textBox1"; 
     this.textBox1.Size = new System.Drawing.Size(156, 20); 
     this.textBox1.TabIndex = 1; 
     // 
     // label2 
     // 
     this.label2.AutoSize = true; 
     this.label2.Location = new System.Drawing.Point(401, 79); 
     this.label2.Name = "label2"; 
     this.label2.Size = new System.Drawing.Size(91, 13); 
     this.label2.TabIndex = 2; 
     this.label2.Text = "age"; 
     // 
     // label3 
     // 
     this.label3.AutoSize = true; 
     this.label3.Location = new System.Drawing.Point(290, 79); 
     this.label3.Name = "label3"; 
     this.label3.Size = new System.Drawing.Size(28, 13); 
     this.label3.TabIndex = 3; 
     this.label3.Text = "old"; 
     // 
     // label4 
     // 
     this.label4.AutoSize = true; 
     this.label4.Location = new System.Drawing.Point(184, 79); 
     this.label4.Name = "label4"; 
     this.label4.Size = new System.Drawing.Size(82, 13); 
     this.label4.TabIndex = 4; 
     this.label4.Text = "sum"; 
     // 
     // label5 
     // 
     this.label5.AutoSize = true; 
     this.label5.Location = new System.Drawing.Point(83, 78); 
     this.label5.Name = "label5"; 
     this.label5.Size = new System.Drawing.Size(28, 13); 
     this.label5.TabIndex = 5; 
     this.label5.Text = "$"; 
     // 
     // textBox2 
     // 
     this.textBox2.Location = new System.Drawing.Point(335, 77); 
     this.textBox2.Name = "textBox2"; 
     this.textBox2.Size = new System.Drawing.Size(44, 20); 
     this.textBox2.TabIndex = 6; 
     // 
     // textBox3 
     // 
     this.textBox3.Location = new System.Drawing.Point(125, 77); 
     this.textBox3.Name = "textBox3"; 
     this.textBox3.Size = new System.Drawing.Size(44, 20); 
     this.textBox3.TabIndex = 7; 
     // 
     // Form2 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(751, 428); 
     this.Controls.Add(this.textBox3); 
     this.Controls.Add(this.textBox2); 
     this.Controls.Add(this.label5); 
     this.Controls.Add(this.label4); 
     this.Controls.Add(this.label3); 
     this.Controls.Add(this.label2); 
     this.Controls.Add(this.textBox1); 
     this.Controls.Add(this.label1); 

對不起,如果這太長了,我該怎麼辦呢?

+0

將它們添加爲WrapPanel的子版本 – raym0nd

+3

您能否重寫您的問題,我無法制作它的正面或反面。什麼是*'根據我的數組'*或*'的幾個例如我有這contcon和我必須加倍陣列的長度這個代碼我必須加倍'* ... – KilZone

回答

1

您可以添加許多控件而不指定名稱。例如:

private void DynamicAddTextBoxes(Point startLocation, count) 
{ 
    Point location = startLocation; 
    for (int textBoxIndex = 0; textBoxIndex < count; textBoxIndex++) 
    { 
     TextBox textBox = new TextBox(); 
     textBox.Text = textBoxIndex.ToString(); 
     textBox.Location = new Point(location.X, location.Y + 50); 
     this.Controls.Add(textBox); 
    } 
} 

此代碼會將TextBox es序列添加到表單中。

0

這是你需要注意的重要組成部分,然後你就會明白如何從你的代碼添加控件:

this.label5 = new System.Windows.Forms.Label(); 

// 
// label5 
// 
this.label5.AutoSize = true; 
this.label5.Location = new System.Drawing.Point(83, 78); 
this.label5.Name = "label5"; 
this.label5.Size = new System.Drawing.Size(28, 13); 
this.label5.TabIndex = 5; 
this.label5.Text = "$"; 

this.Controls.Add(this.label5); 

我只取你張貼,並過濾它的代碼。希望這可以幫助。