Solved

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

Posted on 2006-11-02
5
772 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
5 Comments
 
LVL 85

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 85

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now