Solved

C# WinForms - What order are controls added to control collection?

Posted on 2006-11-02
5
778 Views
Last Modified: 2008-02-01
I have a C# WinFroms Application with the following code:

for (int i = 0; i < this.Controls.Count; i++)
  {
    if (this.Controls[i] is ListBox)
      {
         Control newCtrl = this.Controls[i];
         ListBox lb = (ListBox)newCtrl;
         lb.ClearSelected();
       }
  }

When I step through the code, the controls do not seem to be in any particular order within the collection.  In order for my code to work correctly, I need to clear certain listboxes, before clearing other listboxes.  Is there a way to designate what order the controls are put into the collection?

Thanks,
Sarah
0
Comment
Question by:spendergrass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17862839
If you know the names before hand, then do something more along these lines:
       
        private void button1_Click(object sender, EventArgs e)
        {
            ListBox lb;
            lb = (ListBox)getControlByName(this, "listBox1");
            lb.ClearSelected();
            lb = (ListBox)getControlByName(this, "listBox2");
            lb.ClearSelected();
            lb = (ListBox)getControlByName(this, "listBox3");
            lb.ClearSelected();
        }

        private Control getControlByName(Control ctl, string ctlName)
        {
            foreach (Control child in ctl.Controls)
            {
                if (child.Name == ctlName)
                    return child;
                else if (child.HasChildren)
                {
                    Control retChild = getControlByName(child, ctlName);
                    if (retChild != null)
                        return retChild;
                }
            }
            return null;
        }

You can of course do more error checking than I did.  Make sure that null was not returned and the the returned control was actually a ListBox before casting it...
0
 
LVL 12

Accepted Solution

by:
RubenvdLinden earned 500 total points
ID: 17862842
It's first come, first serve.

Please check the methods which adds the controls to your WinForm application (normally, this would be InitializeComponent()).
There will be several statements like: this.Controls.Add(this.yourControl).
As I said, it's first come, first serve. If you change the order of these statements, the placement in the collection will be different too.

I hope this will help you.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17862846
But after seeing that...if you have a specific order and know the names, then why not just do it directly?

    listBox1.ClearSelected();
    listBox2.ClearSelected();
    listBox3.ClearSelected();
0
 
LVL 8

Expert Comment

by:sabeesh
ID: 17864078
Hi
       you can set "Tag" for your listbox and with respect the tag name you can clear or do whatever you want.
0
 

Author Comment

by:spendergrass
ID: 17866455
I was hoping to be able to keep the code that I posted generic - not having to specify the order within that section of code.  Changing the order the controls are added to the form will allow me to do this.  Thanks everyone for your help!

Sarah
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question