0

I'm trying to put dynamicaly a LinkLabel (also I traied to put a button) in a TabPage:

LinkLabel newLinkLabelButton = new LinkLabel();
newLinkLabelButton.Text = "Login";
newLinkLabelButton.Name = "linkLabel_11";
tabs.TabPages[0].Controls.Add(newLinkLabelButton);

Now I'm trying to find this control on the specificf TabPage with function

newLoginLinkLabel = (LinkLabel)Helper.GetLinkLabelByTagAndfamily(tabs.TabPages[0], _name);

where the function body is:

public static Control GetControlByTagAndfamily(TabPage _tab, string _name)
{
  Control rez = new Control();
  foreach (Control ctrl in _tab.Controls)
  {
      if (ctrl.Name == _name)
      {
        rez = ctrl;
        break;
      }
   }
   return rez;
}

But the function never founds a LinkLabel or a Button inside _tab.Controls collection. I observed the collection contains founds Labels only, if I trying to find some labels inside.

Pleas help to solve this.

kirpi4
  • 143
  • 1
  • 1
  • 11
  • 1
    At what point of the page life cycle are you adding the dynamic controls? And at what point are you trying to find them? – Andrei Jan 16 '14 at 11:25
  • 3
    By the way, `if (ctrl.Name == _name) return ctrl;` would suffice, no need for `break`; – Ondrej Janacek Jan 16 '14 at 11:32
  • @Andrei I add LinkLabel at on Form1_Load. Then, I'm tryind to find-it on some button Click event. – kirpi4 Jan 16 '14 at 12:11
  • You could rewrite the body of the helper-method to a much shorter solution: `return _tabl.Controls.OfType().SingleOrDefault(c => c.Name == _name)` for finding a LinkLabel or `return _tabl.Controls.Cast().SingleOrDefault(c => c.Name == _name)` for any control. – Abbas Jan 16 '14 at 13:48

1 Answers1

0

Well, my problem is solved, the code above is correct. The problem was in wrong _name calculation before using it in

GetControlByTagAndfamily(TabPage _tab, string _name);
kirpi4
  • 143
  • 1
  • 1
  • 11