Create textboxs in a user-determined amount?

Asked

Viewed 32 times

0

In a form the user will be able to choose the amount of images he can put, for each image I must create a texbox to put the URL of each image. He will determine this amount through a NumericUpDown, then I have to read its value and apply it to the function to create the amount of textbox:

I tried so:

private void criaImg(TextBox[] txt,  int X, int Y, int qnt)
{
    int cont = 0;
    while (cont < qnt)
    {
        txt[cont] = new System.Windows.Forms.TextBox();
        txt[cont].Location = new System.Drawing.Point(X, Y + Y);
        txt[cont].Name = "img" + cont;
        txt[cont].Size = new System.Drawing.Size(100, 20);
        txt[cont].TabIndex = 1;
        cont++;
    }

}
private void qntImg_ValueChanged(object sender, EventArgs e)
{
    TextBox[] array = new TextBox[(int)qntImg.Value];
    criaImg(array, 20, 30, (int)qntImg.Value);

}

Perhaps it would be simpler to put a button ADD and when the user clicks there it creates a textbox but it doesn’t work, how do I do it?

1 answer

0

You created Textbox, but you didn’t add it to the form.

See an example with a button:

private void InitializeComponent()
{
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(13, 13);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(75, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.UseVisualStyleBackColor = true;
    //
    // Form1
    //
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(292, 273);

    // Veja no seu código onde fica este método ou similar e faça também!
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.