Solved

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

Posted on 2006-11-02
5
777 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 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