Create textboxes in a quantity determined by the user?

0

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

I tried this way:

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

}

Maybe it would be simpler to put a ADD button and when the user clicks it it creates a textbox but it does not work, how do I do it?

    
asked by anonymous 20.09.2015 / 16:28

1 answer

0

You created the TextBox, but you did not add it to 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);
}
    
20.09.2015 / 16:45