Reputation: 117
I'm trying to dynamically create objects, and then call from them later. For example...
for(int i=0;i<10;i++)
{
tabControl.TabPages.Add(i, i.ToString());
richTextBox rtb = new richTextBox();
rtb.Parent = tabControl.TabPages[i];
rtb.dock = fill;
}
then later in my coding..
private void onButtonClick_example()
{
var rtb = tabControl.SelectTab.GetChildrenByPoint(new point(1,1));
rtb.WordWrap = true;
}
How can I return that child as a "rich text box" again?
Upvotes: 1
Views: 84
Reputation: 209
Loop through the controls in the selected tab:
foreach(var control in tabControl.SelectTab.Controls)
{
if(control.ID == "NEWCONTROLID")
{
RichTextBox rtb = (RichTextBox) control;
}
}
Did this off the top of my head, so there may be code issues, but hopefully it puts you on the path. Basically you need to search for the control you created in the controls collection of the selected tab, then cast it as a RichTextBox.
You could also use Control.Find() method to find your control and then cast it.
Upvotes: 0
Reputation: 38374
If GetChildrenByPoint returns something other than RichTextBox, then you need to use as
and check for null so you don't crash when other controls are encountered.
foreach(var item in tabControl.SelectTab.GetChildrenByPoint(new point(1,1)))
{
RichTextBox rtb= item as RichTextBox;
if(rtb != null) //if we found a RichTextBox
{
rtb.WordWrap = true;
}
}
Upvotes: 1