Find Child Control

S

shapper

Hello,

I have a control named Parent in my page.
Parent has many child controls under it which also have other child
controls under them.

I need to find a control named "A" which i don't know exactly where it
is.
I just know that is under parent or under any of Parent Child
controls ...

How can I do this?

Thanks,
Miguel
 
B

bruce barker

trival:

// find all child controls named "A"

Control[] list = ControlWalker(myControl, delegate(Control ctl)
{
return ctl.ID != null && ctl.ID == "A";
});

......

// control walker method

public delegate bool ControlWalkerMatcher (Control ctl);
public Control[] ControlWalker(
Control ctl,
ControlWalkerMatcher matcher)
{
ArrayList list = new ArrayList();
if (matcher(ctl)) list.Add(ctl);
for (int i=0; i < ctl.Controls.Count; ++i)
{
Control[] childList =ControlWalker(ctl.Controls,matcher);
if (childList.Length > 0) list.AddRange(childList);
}
return (Control[]) list.ToArray(typeof(Control));
}

-- bruce (sqlwork.com)
 
S

shapper

trival:

// find all child controls named "A"

Control[] list = ControlWalker(myControl, delegate(Control ctl)
{
return ctl.ID != null && ctl.ID == "A";
});

......

// control walker method

public delegate bool ControlWalkerMatcher (Control ctl);
public Control[] ControlWalker(
Control ctl,
ControlWalkerMatcher matcher)
{
ArrayList list = new ArrayList();
if (matcher(ctl)) list.Add(ctl);
for (int i=0; i < ctl.Controls.Count; ++i)
{
Control[] childList =ControlWalker(ctl.Controls,matcher);
if (childList.Length > 0) list.AddRange(childList);
}
return (Control[]) list.ToArray(typeof(Control));
}

-- bruce (sqlwork.com)
I have a control named Parent in my page.
Parent has many child controls under it which also have other child
controls under them.
I need to find a control named "A" which i don't know exactly where it
is.
I just know that is under parent or under any of Parent Child
controls ...
How can I do this?
Thanks,
Miguel


Hi,

Your code is a little bit confusing to me. Why a delegate?
Can't a single function perform the action of searching for the
control?

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,888
Messages
2,569,964
Members
46,294
Latest member
HollieYork

Latest Threads

Top