Home All Groups Group Topic Archive Search About

Modifiers of controls in UserControls

Author
4 May 2006 9:06 AM
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

Author
4 May 2006 6:18 PM
Eric Cadwell
How about deleting this line?

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

Show quote
:)
Author
4 May 2006 7:22 PM
Eric Cadwell
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[i].FieldType))
        {
            col.Add((Control)fields[i].GetValue(ctl));
            AddContainerControls2((Control)fields[i].GetValue(ctl), col);
        }
    }
}

HTH,
Eric
Author
5 May 2006 7:28 AM
Andreas
fields.Length is allways 0
Author
5 May 2006 2:23 PM
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[i].FieldType))
        {
            Control current = (Control)fields[i].GetValue(ctl);

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



-Eric

AddThis Social Bookmark Button