4 Replies - 550 Views - Last Post: 07 August 2012 - 12:25 AM Rate Topic: -----

#1 ralphbandian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-August 12

find controls in two containers using 1 foreach

Posted 06 August 2012 - 11:24 PM

i'm trying to access controls in two containers...I could do it by using 2 foreach statement..

e.g

foreach(Control but in panel1.Controls)
{
  if(but is Button)
  {
    but.BackColor = Colors.Green;
  }
}
foreach(Control but in panel2.Controls)
{
  if(but is Button)
  {
    but.BackColor = Colors.Red;
  }
}



is there an easier way? or how do I do it using only 1 foreach statement?

Is This A Good Question/Topic? 0
  • +

Replies To: find controls in two containers using 1 foreach

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: find controls in two containers using 1 foreach

Posted 06 August 2012 - 11:47 PM

Why do you need it in 1 foreach statement?
Was This Post Helpful? 0
  • +
  • -

#3 ralphbandian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-August 12

Re: find controls in two containers using 1 foreach

Posted 06 August 2012 - 11:56 PM

View PostSkydiver, on 06 August 2012 - 11:47 PM, said:

Why do you need it in 1 foreach statement?



..uhh..the thing is I have to repeat this process again for the other containers..and I have 3 containers in my form and my code for the first container is already long...so I was wondering if, instead of copying and pasting the code 2 more times, I could do it in one foreach statement that accesses controls in 3 containers..
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: find controls in two containers using 1 foreach

Posted 07 August 2012 - 12:04 AM

You could write a helper method:
void SetPanelButtonsBackColor(Panel panel, Color color)
{
    foreach(Control control in panel.Controls)
    {
        if(control is Button)
            control.BackColor = color;
    }
}


Was This Post Helpful? 4
  • +
  • -

#5 ralphbandian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-August 12

Re: find controls in two containers using 1 foreach

Posted 07 August 2012 - 12:25 AM

it worked...thank you very much sir!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1