473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Enumerating non-ui components from the given form instance

Hi,

As you know, all the non-ui components (like Timer control, etc.) that sit
on the form are contained in its private variable 'components'.

How can I enumerate such components from any form instance given to me? I
have no access to the source of the given form instance, but I can impose
that the form be inherited from certain base class I provide.

Would appreciate any inputs. If there is other way to access the non-ui
components (other than using the 'components' variable), would appreciate to
hear that.

thanks!
-Yasutaka

Jul 21 '05 #1
4 1771
Thanks Dinesh. This will work if you're searching it within the same form,
but
what I want is an instance of form that's given to me.

For example, I have a routine like this...

public void EnumerateComponents(System.Windows.Forms.Form anyFormInstance)
{
foreach (System.ComponentMode.Component component In
anyFormInstance.components)
{
System.Diagnostics.Debugs.WriteLine(component.ToSt ring());
}
}

But, this routine will not work, as the 'components' is private variable of
the
anyFormInstance. I have no access to the source code of the anyFormInstance.
At the most, I can creator a base form, which I can impose to the author of
the
Form (that's passed to the anyFormInstance parameter) to inherit from.

It'd be too bad if I can't have an access to the components... do you think
Reflection has something in store? I don't have much idea bout this form...

thanks!
-Yasutaka

"Dinesh Priyankara" <di****@dineshpriyankara.com> wrote in message
news:74**********************************@microsof t.com...
Hi Yasutaka,

See whether this code can be used

foreach (object o in this.components.Components)
{
Timer t = (Timer)o;
MessageBox.Show(t.Interval.ToString());
}


Hi,

As you know, all the non-ui components (like Timer >>control, etc.) that siton the form are contained in its private variable >>'components'.

How can I enumerate such components from any >>form instance given to me? Ihave no access to the source of the given form >>instance, but I can imposethat the form be inherited from certain base class I >>provide.

Would appreciate any inputs. If there is other way to access the non-ui
components (other than using the 'components' variable), would appreciate tohear that.

thanks!
-Yasutaka


Jul 21 '05 #2
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 EnumerateComponents
function passing the instance of Form2.

private void button1_Click(object sender, System.EventArgs e)
{
Form2 form = new Form2();
ComponentsCollection(form);
}

public void EnumerateCollection(System.Windows.Forms.Form anyFormInstance)
{
foreach(System.ComponentModel.Component component in
anyFormInstance.Site.Container.Components)
{
MessageBox.Show(component.ToString());
}
}

I must be getting something screwed up here... would appreciate any help.

thanks!
-Yasutaka

"Iulian Ionescu" <an*******@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.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 IReferenceService (get it using Site.GetSevice) and use its
methods to make the query...
Hope this helps,
iulian


Jul 21 '05 #3
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 ComponentsIterator(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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;

namespace Iterator
{
/// <summary>
/// Summary description for ComponentsEnumerator.
/// </summary>
public class ComponentsIterator: IEnumerable, IEnumerator
{
private Control mTarget;
private IEnumerator mCurrentEnumerator;
private bool mInherited;
private FieldInfo mComponentsField = null;
public ComponentsIterator(Control ctrl, bool inherited)
{
mTarget = ctrl;
mInherited = inherited;
Reset();
}

private void InitializeEnumerator()
{
mCurrentEnumerator = null;
if(mComponentsField != null)
{
IContainer components = mComponentsField.GetValue(mTarget) as
IContainer;
if(components != null)
{
mCurrentEnumerator = components.Components.GetEnumerator();
}
}
}
#region IEnumerable Members

public IEnumerator GetEnumerator()
{
return this;
}

#endregion

#region IEnumerator Members

public void Reset()
{
mComponentsField = mTarget.GetType().GetField("components",
BindingFlags.NonPublic | BindingFlags.Instance);
InitializeEnumerator();

}

public object Current
{
get
{

if(mCurrentEnumerator == null) throw new InvalidOperationException();
return mCurrentEnumerator.Current;
}
}

public bool MoveNext()
{
if(mCurrentEnumerator == null) throw new InvalidOperationException();
bool res = mCurrentEnumerator.MoveNext();
while(!res && mCurrentEnumerator != null)
{
if(mInherited)
{
mComponentsField =
mComponentsField.DeclaringType.BaseType.GetField(" components",
BindingFlags.NonPublic | BindingFlags.Instance);
InitializeEnumerator();
if(mCurrentEnumerator != null) res = mCurrentEnumerator.MoveNext();
}
else break;
}
return res;
}

#endregion
}
}

--
B\rgds
100
"Yasutaka Ito" <no****@nonexistent.com> wrote in message
news:O%****************@TK2MSFTNGP11.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 EnumerateComponents
function passing the instance of Form2.

private void button1_Click(object sender, System.EventArgs e)
{
Form2 form = new Form2();
ComponentsCollection(form);
}

public void EnumerateCollection(System.Windows.Forms.Form anyFormInstance) {
foreach(System.ComponentModel.Component component in
anyFormInstance.Site.Container.Components)
{
MessageBox.Show(component.ToString());
}
}

I must be getting something screwed up here... would appreciate any help.

thanks!
-Yasutaka

"Iulian Ionescu" <an*******@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.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 IReferenceService (get it using Site.GetSevice) and use its
methods to make the query...

Hope this helps,
iulian

Jul 21 '05 #4
Hey, thanks a lot for that... I have done in a similar manner. Here is what
I ended
up with. I'm using GetField(), as I know that it is a field, and I'm taking
the default
field name 'components', to make the search simple and faster.

// myForm is an instance of System.Windows.Forms.Form
FieldInfo fieldInfo = myForm.GetType().GetField("components",
BindingFlags.NonPublic |
BindingFlags.Instance);

if (fieldInfo != null)
{
System.ComponentModel.IContainer components =
(System.ComponentModel.IContainer)fieldInfo.GetVal ue(myForm);
if (components != null)
{
return components;
}
}
return null;

thanks to all for the help.
-Yasutaka

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:eZ**************@TK2MSFTNGP09.phx.gbl...
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.


Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bishman | last post by:
Can anyone suggest a method of enumerating instances of MSDE 2000 without using DMO ? Using C++, MFC, and ADO. Thanks
1
by: Kevin | last post by:
Hi, How can I enumerate a linked list while being hable to delete any number of elements while enumerating ? I was using: struct st { ... struct st *prev;
5
by: John Wood | last post by:
Does anyone know how to enumerate the managed threads in the current process? Process.Threads gives you a list of ProcessThreads, but I want to somehow get a list of the managed threads... --...
1
by: José Achig | last post by:
Hi all, The service I have written can not enumerate the current user's desktop windows when running as a service. I read where one solution to this is to install the service with...
2
by: Tony | last post by:
I have this problem - I have a hashtable, containing a list of filenames. Every 60 seconds, I have a thread that enumerates thru this hashtable, and based on some simple logic, some of the items...
1
by: Glenn Leifheit | last post by:
Does anyone have any sample code on enumerating network shares with vb.net. I here you need to use wither an API or WMI, or are there other recomendations. Thanks Glenn
1
by: Shelby | last post by:
Hi, I would like to modify the object's value while enumerating but I get this error Additional information: Collection was modified; enumeration operation may not execute. This is my code: ...
1
by: Phil Galey | last post by:
Is there any way of enumerating all printers, including network printers in VB.NET? printing.PrinterSettings.InstalledPrinters only enumerates printers attached to the local workstation. ...
1
by: Jay | last post by:
Hey There, I am trying to get a list of the open windows from a Windows Service. My initial code was written in a regular server and the window listing function EnumWindows worked fine, but when I...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.