Sossenbinder
Sossenbinder

Reputation: 5282

How can I access the children of a panel element?

currently, my Form looks like this:

enter image description here

it's a panel with 2 labels inside. They are clearly linked to the panel, as on removal of the panel both labels are being removed.

However, I can't access the children of the panel when I try it in my code.

In my Form class, I would have guessed I would have to go with this:

this.panelName.label1

or

this.panelName.Children

or

this.panelName.Items

however, I can't find out how to get access to the labels withing. All of the items are public.

Any advice?

Upvotes: 1

Views: 5811

Answers (3)

Pier Giorgio Misley
Pier Giorgio Misley

Reputation: 5351

I had same problem some days ago, i did this:

var elements = new List<FrameworkElement>();

foreach (FrameworkElement element in yourContent.Children)
{
    // your code here
    //example:
    elements.Add(element);
    // more blabla code
}

EDITED, addes easier names.

it worked for me, hope it helps you!

Upvotes: -1

LInsoDeTeh
LInsoDeTeh

Reputation: 1038

You can acccess controls inside a panel via

this.panelName.Controls

You can use

this.panelName.Controls.Find()

to find your labels or just access them via the index Controls[i]

Upvotes: 2

Andrey Korneyev
Andrey Korneyev

Reputation: 26846

You should be able just access your label by name, like this.label1.

The fact of label laying "on panel" doesn't matter in this case, it means only what this label was added into panelName.Controls collection of panel's child controls, but this label is still member of your class and can be accesssed by name directly.

Upvotes: 3

Related Questions