Iterating through a form's components?

D

David Veeneman

Is there a way to iterate the components on a form?

I need to determine whether an instance of my custom component (a
System.ComponentModel component) is present in a form. I have tried
iterating the Controls collection, but components do not appear to be in
that collection-- I think it's because components inherit from
MarshalByRefObject, rather than Control.

So, how do I find a form's components collection? Thanks in advance.
 
P

Phil

Is there a way to iterate the components on a form?

I need to determine whether an instance of my custom component (a
System.ComponentModel component) is present in a form. I have tried
iterating the Controls collection, but components do not appear to be in
that collection-- I think it's because components inherit from
MarshalByRefObject, rather than Control.

So, how do I find a form's components collection? Thanks in advance.


foreach (Component x in components.Components) {
}


Will this do it? I have never used the "components" property but the
forms designer uses it in this method (below) which it writes. Be
aware that components can be null.


/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
 

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