473,401 Members | 2,125 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,401 software developers and data experts.

GetEnumerator not showing all controls

I am using GetEnumerator for determining how many ListBox controls exist
on a form and then binding each of them to a datasource.

This is required as I need to add more Listbox controls to the form as
required.

All my controls are setup similar to the following:

private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TabControl tabControl1;

and they are global to a class called "MainForm".

As a part of this class, there is a method:
private void BindListBoxes()

I call this method from the "Mainform" constructor.

Within BindListBoxes(), is the following code:

private void BindListBoxes()
{
string controlType;
IEnumerator posControls = Controls.GetEnumerator();

while (posControls.MoveNext())
{
Control posControl = (Control) posControls.Current;
controlType = posControl.Name;
MessageBox.Show(controlType);
}
}

This results in only one of the controls being displayed in the
MessageBox: The "tabControl1" control. I am having trouble determing how
to write the code necessary to see all of the other controls on the
form.

Incidentally, the code above is just an initial phase of developing what
is required to bind a datasource to each ListBox.

Any ideas?

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
1 2161
MikeB <no****@devdex.com> wrote in
news:uI**************@TK2MSFTNGP12.phx.gbl:
I am using GetEnumerator for determining how many ListBox
controls exist on a form and then binding each of them to a
datasource.

This is required as I need to add more Listbox controls to the
form as required.

All my controls are setup similar to the following:

private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TabControl tabControl1;

and they are global to a class called "MainForm".

As a part of this class, there is a method:
private void BindListBoxes()

I call this method from the "Mainform" constructor.

Within BindListBoxes(), is the following code:

private void BindListBoxes()
{
string controlType;
IEnumerator posControls = Controls.GetEnumerator();

while (posControls.MoveNext())
{
Control posControl = (Control) posControls.Current;
controlType = posControl.Name;
MessageBox.Show(controlType);
}
}

This results in only one of the controls being displayed in the
MessageBox: The "tabControl1" control. I am having trouble
determing how to write the code necessary to see all of the
other controls on the form.

Incidentally, the code above is just an initial phase of
developing what is required to bind a datasource to each
ListBox.

Any ideas?


Mike,

WinForms controls can contain other controls, which can contain more
controls, ad infinitum.

You have to recursively process the Controls property of a control
(and the controls it owns, etc.) to build a hierarchical "tree" of
control ownership.

It's not hard. To demonstrate, use this code:
1. Put these instance variables in your form's class:

private string _controlNames = string.Empty;
private int _indentation = 0;

2. Put this code in the form's OnLoad handler or in a button's
handler:

this.AddControlNames(this.Controls);
MessageBox.Show(this._controlNames);

3. Put this recursive method in the form's class:

private void AddControlNames(Control.ControlCollection cc)
{
foreach (Control c in cc)
{
this._controlNames += new string(' ', this._indentation) +
c.Name + Environment.NewLine;

this._indentation += 2;
this.AddControlNames(c.Controls);
this._indentation -= 2;
}
}

Running this code will display a message box showing all of the names
of the controls on the form, like this:
tabControl1

tabPage1

label1

tabPage2

groupBox2

label2

checkBox1

groupBox1

listBox1

button1

textBox1

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 17 '05 #2

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

Similar topics

1
by: Henry | last post by:
I was trying to work through the Lab 2 exercises in the Microsoft's Windows-Based Applications in Visual C#. While I am not showing any errors I am also not seeing the mainMenu object when I run...
4
by: John C | last post by:
I'm new to C#, so just point me at the correct reference material if this question has been answered before. When creating a new class which implements the IDictionary interface, two versions of...
9
by: Ty Moffett | last post by:
Is there anything obvious wrong with this little loop? The second statement throws an exception "Enumeration has not started. Call MoveNext." I thought that is what I did in line one. My...
2
by: Sam Marrocco | last post by:
I've constructed a class that inherits the NameObjectCollectionBase class. All works well, but I'd like to shadow the GetEnumerator method so that it returns an actual value *instead of a...
2
by: Steve Richter | last post by:
very confused on how to implement the IEnumerable and IEnumerator interfaces on a generic type. I understand I should just use a foreach loop in the GetEnumerator method and use "yield return",...
2
by: =?Utf-8?B?VGVk?= | last post by:
I have been asked to change an existing program which Implemented a Collection Class The current class had what is below (its just a summary of the code) Public Class FieldList Implements...
16
by: colin | last post by:
Hi, is it possible to have a recursive GetEnumerator for traversing a tree structure ? public IEnumerator<DTypeGetEnumerator() { return GetEnumerator(root);
2
by: Tony | last post by:
Hello! According to the documentation we have the following interface IList : ICollection, IEnumerable { I can understand all the methods in this IList } interface ICollection : IEnumerable...
1
by: nRk | last post by:
Hi, I am working on asp.net 2.0 with Visual Studio 2005. My asp.net project contains one Masterpage. I exposed my asp.net project in my machine (Windows XP with IIS 5.1) and access using...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.