473,796 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1812
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 EnumerateCompon ents(System.Win dows.Forms.Form anyFormInstance )
{
foreach (System.Compone ntMode.Componen t component In
anyFormInstance .components)
{
System.Diagnost ics.Debugs.Writ eLine(component .ToString());
}
}

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****@dineshp riyankara.com> wrote in message
news:74******** *************** ***********@mic rosoft.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.ToS tring());
}


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 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


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 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

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("com ponents",
BindingFlags.No nPublic |
BindingFlags.In stance);

if (fieldInfo != null)
{
System.Componen tModel.IContain er components =
(System.Compone ntModel.IContai ner)fieldInfo.G etValue(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******** ******@TK2MSFTN GP09.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
1686
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
1590
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
5521
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... -- John Wood Blog: http://spaces.msn.com/members/johnwood/
1
1749
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 "SystemAccount" rights and "Allow service to interact with desktop" but I know it leaves the system vulnerable to maliciousness. Is there a way to programmatically enumerate through the current desktop
2
4576
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 in the hashtable has to be removed. But when I remove a pair from the hashtable, inside the enumeration loop, I get an exception. Why cant I remove from a hashtable, while enumerating ?
1
3334
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
1323
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: Dim icom as MyOwnStructureObj Dim MyEnum As IDictionaryEnumerator = Myhash.GetEnumerator()
1
2226
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. Thanks.
1
1136
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 ported my code over to the service, then it stopped working. I saw that this is due to a service running on a different "non-interactive" dekstop than a regular user desktop. How would I get a service to just get the window handles of the...
10
6773
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 large number) have to be removed (say when ToBeRemoved = true). class SomeObj ToBeRemoved be a boolean field end class
0
9529
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9054
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.