Hi Yasutaka,
I wrote a simple iterator class for you. You can use it to get the
compontets your controls host. You may use the code as is, but bare in mind
that it will work only for the controls generated with VS wizard because it
looks for the *components* member. And of course if you want to use it as is
I'll suggest you do do some more testing because I haven't done enough. The
code is at the end of this post.
You can use the class like this
foreach( Component c in new ComponentsItera tor(control, false))
{
....
}
If the secont constructor's parameter is *false* the interator will
iterates only over the components from the controls' class
If true it will iterate over the parent classes as well.
The class:
===========
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Reflecti on;
namespace Iterator
{
/// <summary>
/// Summary description for ComponentsEnume rator.
/// </summary>
public class ComponentsItera tor: IEnumerable, IEnumerator
{
private Control mTarget;
private IEnumerator mCurrentEnumera tor;
private bool mInherited;
private FieldInfo mComponentsFiel d = null;
public ComponentsItera tor(Control ctrl, bool inherited)
{
mTarget = ctrl;
mInherited = inherited;
Reset();
}
private void InitializeEnume rator()
{
mCurrentEnumera tor = null;
if(mComponentsF ield != null)
{
IContainer components = mComponentsFiel d.GetValue(mTar get) as
IContainer;
if(components != null)
{
mCurrentEnumera tor = components.Comp onents.GetEnume rator();
}
}
}
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return this;
}
#endregion
#region IEnumerator Members
public void Reset()
{
mComponentsFiel d = mTarget.GetType ().GetField("co mponents",
BindingFlags.No nPublic | BindingFlags.In stance);
InitializeEnume rator();
}
public object Current
{
get
{
if(mCurrentEnum erator == null) throw new InvalidOperatio nException();
return mCurrentEnumera tor.Current;
}
}
public bool MoveNext()
{
if(mCurrentEnum erator == null) throw new InvalidOperatio nException();
bool res = mCurrentEnumera tor.MoveNext();
while(!res && mCurrentEnumera tor != null)
{
if(mInherited)
{
mComponentsFiel d =
mComponentsFiel d.DeclaringType .BaseType.GetFi eld("components ",
BindingFlags.No nPublic | BindingFlags.In stance);
InitializeEnume rator();
if(mCurrentEnum erator != null) res = mCurrentEnumera tor.MoveNext();
}
else break;
}
return res;
}
#endregion
}
}
--
B\rgds
100
"Yasutaka Ito" <no****@nonexis tent.com> wrote in message
news:O%******** ********@TK2MSF TNGP11.phx.gbl. ..
Thanks Iulian!
I tried this, but this always return me null. :( I'm trying the following.
1) I have Form1 with a button
2. In the button's Click() event, I'm calling my EnumerateCompon ents
function passing the instance of Form2.
private void button1_Click(o bject sender, System.EventArg s e)
{
Form2 form = new Form2();
ComponentsColle ction(form);
}
public void EnumerateCollec tion(System.Win dows.Forms.Form
anyFormInstance ) {
foreach(System. ComponentModel. Component component in
anyFormInstance .Site.Container .Components)
{
MessageBox.Show (component.ToSt ring());
}
}
I must be getting something screwed up here... would appreciate any help.
thanks!
-Yasutaka
"Iulian Ionescu" <an*******@disc ussions.microso ft.com> wrote in message
news:BC******** *************** ***********@mic rosoft.com... Read the Site of a control that exists on the form or of the form
itself. Check the Container member of the Site and it will contain a list of
available components. If you need the type of the components you will need
a reference to a IReferenceServi ce (get it using Site.GetSevice) and use its
methods to make the query...
Hope this helps,
iulian