Modifiers of controls in UserControls

A

Andreas

Hello

I have a problem with the visibility of private controls in my
UserControl.
My UserControl contains a private PictureBox and a private Label.
When I put my UserControl on a form and get all controls of the form
with:

....
Dim myControls As ArrayList
myControls = AllControls(Me)
....

Public Function AllControls(ByVal frm As Form) As ArrayList
Dim colControls As New ArrayList
AddContainerControls(frm, colControls)
Return colControls
End Function

Private Sub AddContainerControls(ByVal ctlContainer As Control,
ByVal colControls As ArrayList)
Dim ctl As Control
For Each ctl In ctlContainer.Controls
colControls.Add(ctl)
AddContainerControls(ctl, colControls)
Next
End Sub

ther is also the PictureBox and the Label from my UserControl and also
my UserControl in myControls.

Has anybody an idea how can get only my UserControl without the
PictureBox and the Label?

Thanks
 
G

Guest

How about deleting this line?

For Each ctl In ctlContainer.Controls
colControls.Add(ctl)
' AddContainerControls(ctl, colControls)
Next

:)
 
G

Guest

I kid, I kid... Have you tried something like this?

private void AddContainerControls(Control ctl, ArrayList col)
{
Type t = ctl.GetType();

FieldInfo[] fields = t.GetFields(BindingFlags.Public |
BindingFlags.Instance);

for (int i = 0; i < fields.Length; i++)
{
Type c = typeof(Control);

if (c.IsAssignableFrom(fields.FieldType))
{
col.Add((Control)fields.GetValue(ctl));
AddContainerControls2((Control)fields.GetValue(ctl), col);
}
}
}

HTH,
Eric
 
E

Eric Cadwell

Ya, it's only looking for Public controls. You can change the binding flags
to suit your needs.

Also, maybe check the Parent is not your control type?


private void AddContainerControls(Control ctl, ArrayList col)
{

Type t = ctl.GetType();

FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance);

for (int i = 0; i < fields.Length; i++)
{
Type c = typeof(Control);

if (c.IsAssignableFrom(fields.FieldType))
{
Control current = (Control)fields.GetValue(ctl);

if (!(current.Parent is UserControl1))
{
col.Add(current);
AddContainerControls(current, col);
}
}
}
}



-Eric
 

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

Top