dynamically controlling windows form objects

hi all
is there a way to dynamically add user defined number(the number is not known in the design time) of TextBox objects to a windows form in runtime

my problem is generating the TextBox object identifier dynamically in the runtime
I am C# beginner &I don't know the c# compiler ristrictions

thanks for cooperation

Comments

  • Yes, no problem.

    [code]
    // in Form1.cs

    // the text boxes
    TextBox[] textBoxes;

    void AddThem(int count)
    {
    this.SuspendLayout(); // not necessary, but makes the new text boxes all appear at once

    textBoxes = new TextBox[count];

    // say we add them all one under the other at x = 10, y = 10 with 10 space betwen them
    for(int i = 0; i < count; i++)
    {
    // initialize it
    textBoxes[i] = new TextBox();
    textBoxes[i].Text = string.Format("TextBox #{0}", i);
    textBoxes[i].Location = new Point(10, 10 + i * textBoxes[i].Height + 10);
    // add it to the form
    this.Controls.Add(textBoxes[i]);
    }

    this.ResumeLayout();
    }
    [/code]

    : hi all
    : is there a way to dynamically add user defined number(the number is not known in the design time) of TextBox objects to a windows form in runtime
    :
    : my problem is generating the TextBox object identifier dynamically in the runtime
    : I am C# beginner &I don't know the c# compiler ristrictions
    :
    : thanks for cooperation
    :

  • dear pingpong
    thanks alot for ur help and support
    : Yes, no problem.
    :
    : [code]
    : // in Form1.cs
    :
    : // the text boxes
    : TextBox[] textBoxes;
    :
    : void AddThem(int count)
    : {
    : this.SuspendLayout(); // not necessary, but makes the new text boxes all appear at once
    :
    : textBoxes = new TextBox[count];
    :
    : // say we add them all one under the other at x = 10, y = 10 with 10 space betwen them
    : for(int i = 0; i < count; i++)
    : {
    : // initialize it
    : textBoxes[i] = new TextBox();
    : textBoxes[i].Text = string.Format("TextBox #{0}", i);
    : textBoxes[i].Location = new Point(10, 10 + i * textBoxes[i].Height + 10);
    : // add it to the form
    : this.Controls.Add(textBoxes[i]);
    : }
    :
    : this.ResumeLayout();
    : }
    : [/code]
    :
    : : hi all
    : : is there a way to dynamically add user defined number(the number is not known in the design time) of TextBox objects to a windows form in runtime
    : :
    : : my problem is generating the TextBox object identifier dynamically in the runtime
    : : I am C# beginner &I don't know the c# compiler ristrictions
    : :
    : : thanks for cooperation
    : :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories