473,325 Members | 2,816 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,325 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 1768
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.