imdrunkisuppose
imdrunkisuppose

Reputation: 131

How can I use text property of dynamic created textfield?

I have this code, which dynamically creates some textfields for me. k is taken from user btw.

        for (int i = 0; i < k; i++) 
        {
            TextBox t1 = new TextBox();
            t1.Parent = groupBox2;
            t1.Left = textBox2.Left;
            t1.Top = textBox2.Top + (i + 1) * 40;
            t1.Name = "text" + (i + 1);
            t1.Enabled = true;
            groupBox2.Controls.Add(t1);
        }

What i want to do is, after this creating phase is done, when the user presses groupbox2's "OK" button, I want to take the created textfields' text properties, but so far I don't know how could this be done, since I gave textfields a name, I tried this but didn't work.

    private void button3_Click(object sender, EventArgs e)
    {
        node1.name = textBox2.Text;

        for (int i = 0; i < k; i++) 
        {
            node1.array[i] = Convert.ToInt32("text"+(i+1).Text);
        }
    }

Any help would be nice, thanks.

Upvotes: 0

Views: 99

Answers (4)

Ken Clark
Ken Clark

Reputation: 2530

Try this method:

private void button3_Click(object sender, EventArgs e)
{
    node1.name = textBox2.Text;
    for (int i = 0; i < k; i++)
    {
        TextBox txtBox = (TextBox)groupBox2.FindControl("text" + (i + 1));
        if (txtBox != null)
        {
            node1.array[i] = txtBox.Text;
        }
    }
}

Upvotes: 2

KF2
KF2

Reputation: 10153

Loop through your text boxes in groupBox1 and get their names,Try this:

List<string> TextBoxesName=new List<string>();
            foreach (Control item in groupBox1.Controls)
            {
                if (item is TextBox)
                {
                    TextBoxesName.Add((item as TextBox).Text);
                }
            }

Upvotes: 1

Haedrian
Haedrian

Reputation: 4328

Easiest solution is to put your listboxes in a collection of some sort

List<ListBox> listboxes = new List<ListBox>();

for (...)
{
...
listboxes.add(listbox);

}

Then you can refer back to them whenever you want

Or since you're adding them to a groupbox, why not go through that collection?

Upvotes: 0

Vladimirs
Vladimirs

Reputation: 8609

Set to your dynamic texboxes ID and than you can do groupBox2.FindControl("dynamic_texbox_id") to get your text box

Upvotes: 0

Related Questions