Modifiers of controls in UserControls

  • Thread starter Thread starter Andreas
  • Start date Start date
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
 
How about deleting this line?

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

:)
 
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
 
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
 
Back
Top