0

Need help finding a TableLayoutPanel added at run time to a tab control of a winform. Please find example code below. Any help would be appreciated.

private void GenerateControls()
{
   TableLayoutPanel tp = new TableLayoutPanel();
   tp.Name = "tpName";
   tab1.Controls.Add(tp);
}

private void findTablePanelControl()
{
    TableLayoutPanel tp = (TableLayoutPanel)this.Controls.Find("tpName", true)[0];
    string name = tp.Name;
}

I receive the follow error message: Index was outside the bounds of the array.

I also tried the following code, but get this error (Object reference not set to an instance of an object) on the "string name =" line:

TableLayoutPanel tpParseSchema = (TableLayoutPanel)this.Controls.Find("tpParseSchema", true).FirstOrDefault();
Paul
  • 93
  • 2
  • 11
  • I found the problem. The provide example code actual work. The problem with my real code was that I mistakenly keyed in the wrong name value for the panel. I ended up figuring this out by recursively stepping through all of the child controls of the tab control. Here is example code of how I did that. – Paul Jun 03 '15 at 16:19

1 Answers1

0

I found the problem. The provided example code actually will work. The problem with my real code was that I mistakenly keyed in the wrong name value for the TableLayoutPanel. I ended up figuring this out by recursively stepping through all of the child controls of the tab control. Here is example code of how I did that.

foreach (Control ctrl in tab1.Controls)
{
   string ctrlname = ctrl.Name;
}
Paul
  • 93
  • 2
  • 11