By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,544 Members | 2,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,544 IT Pros & Developers. It's quick & easy.

Adding controls at Runtime

P: n/a
I've got a Windows form that has a TabControl. I add TabPages to this
TabControl at Runtime, along with two Textboxes and a button. The problem
I'm having is I can't figure out how to reference the Textboxes elsewhere
once they have been created. The TextBoxes are being named based on a
private global counter (i.e. TextBox tb = new TextBox(); tb.Name = "tb" +
(counter++).ToString();) They are being created in a private method called
"AddTab". Anybody have any idea's?
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi slylos,

You can either add the TextBoxes to a list of your own or use the TabPage.Controls property to look for a Control with the right name.

On Sat, 16 Jul 2005 19:29:01 +0200, slylos <sl****@discussions.microsoft.com> wrote:
I've got a Windows form that has a TabControl. I add TabPages to this
TabControl at Runtime, along with two Textboxes and a button. The problem
I'm having is I can't figure out how to reference the Textboxes elsewhere
once they have been created. The TextBoxes are being named based on a
private global counter (i.e. TextBox tb = new TextBox(); tb.Name = "tb" +
(counter++).ToString();) They are being created in a private method called
"AddTab". Anybody have any idea's?


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2

P: n/a
That's what I can't figure out - how to create this "List". Also, using
TabPage.Controls property means I have to know the index of the textbox I'm
working with. Is there no way to keep track of these names, then use them at
runtime? i.e.

private void addcontrol()
{
Textbox t = new Textbox();
t.Name = "myBox";
tabPage1.Controls.Add(t);
}

private void SomeOtherMethod()
{
myBox.Text = "Hey!!";
}

That's what I'm hoping for. If not, some way to achieve that . . .

"Morten Wennevik" wrote:
Hi slylos,

You can either add the TextBoxes to a list of your own or use the TabPage.Controls property to look for a Control with the right name.

On Sat, 16 Jul 2005 19:29:01 +0200, slylos <sl****@discussions.microsoft.com> wrote:
I've got a Windows form that has a TabControl. I add TabPages to this
TabControl at Runtime, along with two Textboxes and a button. The problem
I'm having is I can't figure out how to reference the Textboxes elsewhere
once they have been created. The TextBoxes are being named based on a
private global counter (i.e. TextBox tb = new TextBox(); tb.Name = "tb" +
(counter++).ToString();) They are being created in a private method called
"AddTab". Anybody have any idea's?


--
Happy coding!
Morten Wennevik [C# MVP]

Nov 17 '05 #3

P: n/a
Just create a TextBox[], or ArrayList, and put all your TabPage TextBoxes in it. Of course, you will need to know the proper index, or loop through this list or the TabPage.Controls collection and search for a specific name.

foreach(Control c in tabPage1.Controls)
{
if(c.Name == "myBox")
c.Text = "Hello World";
}

or using an ArrayList add your TextBoxes to this list.

foreach(TextBox tb in myArrayList)
{
if(tb.Name == "myBox")
tb.Text = "Hello World";
}

On Sun, 17 Jul 2005 00:07:02 +0200, slylos <sl****@discussions.microsoft.com> wrote:
That's what I can't figure out - how to create this "List". Also, using
TabPage.Controls property means I have to know the index of the textbox I'm
working with. Is there no way to keep track of these names, then use them at
runtime? i.e.

private void addcontrol()
{
Textbox t = new Textbox();
t.Name = "myBox";
tabPage1.Controls.Add(t);
}

private void SomeOtherMethod()
{
myBox.Text = "Hey!!";
}

That's what I'm hoping for. If not, some way to achieve that . . .

"Morten Wennevik" wrote:
Hi slylos,

You can either add the TextBoxes to a list of your own or use the TabPage.Controls property to look for a Control with the right name.

On Sat, 16 Jul 2005 19:29:01 +0200, slylos <sl****@discussions.microsoft.com> wrote:
> I've got a Windows form that has a TabControl. I add TabPages to this
> TabControl at Runtime, along with two Textboxes and a button. The problem
> I'm having is I can't figure out how to reference the Textboxes elsewhere
> once they have been created. The TextBoxes are being named based on a
> private global counter (i.e. TextBox tb = new TextBox(); tb.Name = "tb" +
> (counter++).ToString();) They are being created in a private method called
> "AddTab". Anybody have any idea's?
>


--
Happy coding!
Morten Wennevik [C# MVP]


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #4

P: n/a
That seems like a good solution, I will try that and post back on here.
Thanks for your help Morten!!

"Morten Wennevik" wrote:
Just create a TextBox[], or ArrayList, and put all your TabPage TextBoxes in it. Of course, you will need to know the proper index, or loop through this list or the TabPage.Controls collection and search for a specific name.

foreach(Control c in tabPage1.Controls)
{
if(c.Name == "myBox")
c.Text = "Hello World";
}

or using an ArrayList add your TextBoxes to this list.

foreach(TextBox tb in myArrayList)
{
if(tb.Name == "myBox")
tb.Text = "Hello World";
}

On Sun, 17 Jul 2005 00:07:02 +0200, slylos <sl****@discussions.microsoft.com> wrote:
That's what I can't figure out - how to create this "List". Also, using
TabPage.Controls property means I have to know the index of the textbox I'm
working with. Is there no way to keep track of these names, then use them at
runtime? i.e.

private void addcontrol()
{
Textbox t = new Textbox();
t.Name = "myBox";
tabPage1.Controls.Add(t);
}

private void SomeOtherMethod()
{
myBox.Text = "Hey!!";
}

That's what I'm hoping for. If not, some way to achieve that . . .

"Morten Wennevik" wrote:
Hi slylos,

You can either add the TextBoxes to a list of your own or use the TabPage.Controls property to look for a Control with the right name.

On Sat, 16 Jul 2005 19:29:01 +0200, slylos <sl****@discussions.microsoft.com> wrote:

> I've got a Windows form that has a TabControl. I add TabPages to this
> TabControl at Runtime, along with two Textboxes and a button. The problem
> I'm having is I can't figure out how to reference the Textboxes elsewhere
> once they have been created. The TextBoxes are being named based on a
> private global counter (i.e. TextBox tb = new TextBox(); tb.Name = "tb" +
> (counter++).ToString();) They are being created in a private method called
> "AddTab". Anybody have any idea's?
>

--
Happy coding!
Morten Wennevik [C# MVP]


--
Happy coding!
Morten Wennevik [C# MVP]

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.