List non-visual component

Asked

Viewed 162 times

4

I have tried several means to list all the non-visual components of a form, such as: OpenDialog, ImageList, TableAdapters and etc. but without success.

To find the controls on the screen, I managed with a Foreach us Controls of the screen, but for these non-visual components, I found nothing.

My current code:

private IEnumerable<Component> EnumerateComponents()
{
    return from field in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
           where typeof (Component).IsAssignableFrom(field.FieldType)
           let component = (Component) field.GetValue(this)
           where component != null
           select component;
}

Any idea how to do this process of listing non-visual components?

  • You got that in that answer, right? http://stackoverflow.com/questions/17171914/find-components-on-a-windows-form-c-sharp-not-controls Does it not work? I just saw that you posted on Soen also: http://stackoverflow.com/questions/27026373/find-components-non-visual-c-sharp If you find a good answer there, put it here for us.

  • Absolutely @bigown. Finding an answer, I put it this way.

  • @danielvillage just performed a test with the code you posted, here appeared the Imagelist and Openfiledialog I put to test.

  • 1

    @Mateusalexandre, this code here is more complete. It’s what I wanted. Take a look: Return this.Gettype(). Getfields(Bindingflags.Instance | Bindingflags.Public | Bindingflags.Nonpublic) . Where(f => typeof(Component).Isassignablefrom(f.Fieldtype)) . Where(f => !typeof(Control).Isassignablefrom(f.Fieldtype)) . Select(f => f.Getvalue(this)) . Oftype<Component>();

1 answer

1

As posted in the comments by the author himself, the code below lists all the non-visual components of a form.

private IEnumerable<Component> EnumerateComponents()
{
    return this.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
            .Where(f => typeof(Component).IsAssignableFrom(f.FieldType))
            .Where(f => !typeof(Control).IsAssignableFrom(f.FieldType))
            .Select(f => f.GetValue(this))
            .OfType<Component>();
}

Originally in Find Components Non Visual C#.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.