开发者

insert dynamically controls in win c#

I need to add some controls dynamically, As the number of elements in my array, how can I put in a dynamic and place them in order, for example I have this controls And I have to double them ,this code I have to double

 this.label1 = new System.Windows.Forms.Label();
        this.tex开发者_如何学CtBox1 = 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);

Sorry if that's too long, how can i do it?


You can add many controls without specifying the name. for instance:

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 );
    }
}

This code will add sequence of TextBoxes to the form.


This is the important part that you need to pay attention to, and then you will understand how to add a control from your code:

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);

I've only taken the code you posted and filtered it. Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜