Newcomb J
Newcomb J

Reputation: 1

How to read values from textboxes I created in one form and send them to another form?

So, I created this textboxes in a form2;

System.Windows.Forms.TextBox[] someTb = new System.Windows.Forms.TextBox[10];

for (int i = 0; i < 10; ++i)
{
    sombeTb[i] = new TextBox();

    textos[i].Location = new System.Drawing.Point(60, 84 + i * 35);
    this.Controls.Add(textos[i]);
}

I need to acces to this TextBoxes (someTb) and send them to another Form

Upvotes: 0

Views: 48

Answers (3)

Newcomb J
Newcomb J

Reputation: 1

Finally I used the Name property to calle the TextBox whenever i want.

System.Windows.Forms.TextBox[] someTb = new System.Windows.Forms.TextBox[10];

for (int i = 0; i < 10; ++i)
{
sombeTb[i] = new TextBox();
someTb[i].Location = new System.Drawing.Point(60, 84 + i * 35);
this.Controls.Add(someTb[i]);

someTb[i].Name = "someName" + i.ToString();
}

And then I can acces to its methods like this

this.Controls["someName" + i].Method

It´s not the best way, but since I was in a hurry it was the one I used.

Upvotes: 0

David Haxton
David Haxton

Reputation: 284

Probably the most simple answer would be to have a static class in your project that has static members of TextBoxs and read/write to the static variable from either form.

Upvotes: 0

gmiley
gmiley

Reputation: 6604

Since you already have them in an array, you can just pass that array to another form. Declare a public TextBox[] TextBoxes { get; set; } on the new form, when the form is instanced, assign the array (or a copy of the array) to the public property. Otherwise, you could do similar with a Dictionary<int, string> that uses the array index as the int key portion, and the TextBox Value in the string value portion. Then pass that collection along to the new form.

Upvotes: 1

Related Questions