GetEnumerator not showing all controls

  • Thread starter Thread starter MikeB
  • Start date Start date
M

MikeB

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top