Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linq Help 1 35
scanning dentists xray (the small ones) 3 45
Filename to be appended with DateTimeof Download 3 47
C# Comparing String Arrays. 5 26
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

12 Experts available now in Live!

Get 1:1 Help Now