Search code examples
c#winformscontrolssplitcontainer

How to get all buttons and labels under splitContainer.Panel2


I want to get the background color of all buttons and labels under splitContainer.Panel2. When I try it I discover I not success to run on any control (under Panel2) I try this code:

foreach (Control c in ((Control)splitContainer.Panel2).Controls)
{
    if ((c is Button) || (c is Label))
        MessageBox.Show("Name: " + c.Name + "  Back Color: " + c.BackColor);
}

How can I get all background colors of all labels and buttons under splitContainer.Panel2 ?

EDIT:

  1. I have some panels in splitcontainer.Panel2 and the buttons and labels are in the panels.
  2. I get only this meesage: "Name: panel_Right Back Color: Color [Transparent]"

Solution

  • you get the message probably because you have a panel under your splitContainer.Panel2 and should do:

    foreach (Control c in ((Control)splitContainer.Panel2).Controls)
    {
        if(c is Panel)
        {
          foreach (Control curr in c.Controls)
          {
             MessageBox.Show("Name: " + curr.Name + "  Back Color: " + curr.BackColor);
          }
        }
    }