I have a user control with multiple pictureboxes and labels on them.
I need to put each Label (lb) on his own Picturebox (pbParent), and the label Backcolor must be transparent to improve PictureBox Image visibility under this picture box.
Picture boxes are named TabFrame0 to TabFrameN, and the Labels are named Label0 to LabelN.
I have a function GetLabelByTag that must return the label searched by its name. It works fine until I make the Label’s parent the PictureBox (see row 8). So, my questions is: how do I find the label if it’s parent become a pictureBox, not my UserControl, which collection do contain it?
void CreateControls()
{
...
newLabel.Name = TAB_PIC_BOX_LABEL_NAME + _id.ToString(); //1
newLabel.Text = _text;//2
newLabel.Tag = _id;//3
newLabel.AutoSize = true;//4
Controls.Add(newLabel);//5
Label lb = GetLabelByTag(_id, TAB_PIC_BOX_LABEL_NAME);//6
PictureBox pbParent = GetPicBoxByTag(_id, TAB_PIC_BOX_CONTROL_NAME);//7
lb.Parent=pbParent;//8
lb.BringToFront();//9
...
}
…
Label GetLabelByTag(int _tag, string _family)
{
Label rez = new Label();
foreach (Control lb in Controls)
{
if (lb.Tag != null)
{
if (((int)lb.Tag == _tag) && (lb.Name == _family + _tag.ToString()))
{
rez = (Label)lb;
}
}
}
return rez;
}
You need to recursively iterate the children’s children if there are any. E.g. like this: