user1594121
user1594121

Reputation: 117

Getting child entity as a type?

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

Answers (2)

ps_md
ps_md

Reputation: 209

  1. Add a dynamic ID to your rich text box control when you create it.
  2. 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

AaronLS
AaronLS

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

Related Questions