Tom
Tom

Reputation: 577

C# Unable to loop through control collection and set properties on a user control in Windows Forms

Hi I have a Forms application that uses a custom control based on a Track bar called colorSlider. This control was obtained from code project and added to the tools in VS2017. All aspects of the control work fine. I can change any properties as I wish. However, the final project is quite large and all of the track bars (50 or so) will be replaced with this custom control. I also want to be able to modify the look and feel of the sliders with user customization skins. So, here is a typical command to change button on the slider.

colorSlider1.ThumbInnerColor = Color.FromArgb(99, 130, 208);

And this works perfectly fine. However I want to do this in a loop. All controls are on a panel called backpanel. Here is an example for changing the forecolor of a button:

        foreach (Panel pnl in backPanel.Controls)
        {
            foreach (Control c in pnl.Controls)
            {
                if (c is Button)
                {
                    c.ForeColor = Color.Black;
                }
            }
        }

This works great. However, if I try this:

        foreach (Panel pnl in backPanel.Controls)
        {
            foreach (Control c in pnl.Controls)
            {
                if (c is ColorSlider.ColorSlider)
                {
                    c.ThumbInnerColor = Color.FromArgb(99, 130, 208);
                }
            }
        }

In this case visual studio gives a syntax error

Error CS1061 'Control' does not contain a definition for 'ThumbInnerColor' and no accessible extension method 'ThumbInnerColor' accepting a first argument of type 'Control' could be found (are you missing a using directive or an assembly reference?)

So anyone have an idea on how to fix this? Thanks

Upvotes: 0

Views: 1079

Answers (3)

Idle_Mind
Idle_Mind

Reputation: 39142

You could also use the Controls.OfType<> option:

        foreach(Panel pnl in backPanel.Controls.OfType<Panel>())
        {
            foreach(ColorSlider cs in pnl.Controls.OfType<ColorSlider>())
            {
                cs.ThumbInnerColor = Color.FromArgb(99, 130, 208);
            }
        }

Upvotes: 3

Paweł Audionysos
Paweł Audionysos

Reputation: 606

or in newer C#

foreach (Panel pnl in backPanel.Controls)
{
    foreach (Control c in pnl.Controls)
    {
        if (c is ColorSlider.ColorSlider s)
        {
            s.ThumbInnerColor = Color.FromArgb(99, 130, 208);
        }
    }
}

Upvotes: 2

Heriberto Lugo
Heriberto Lugo

Reputation: 643

look at the error.. Control' does not contain a definition for 'ThumbInnerColor' .. which is true.. Control does not contain that property. now look at your code:

c.ThumbInnerColor = Color.FromArgb(99, 130, 208);

you are trying to set the property on a Object which is type Control.. that property belongs to a specific control of type ColorSlider..

the fix should be as easy as casting the Control to ColorSlider

foreach (Panel pnl in backPanel.Controls)
    {
        foreach (Control c in pnl.Controls)
        {
            ColorSlider.ColorSlider slider = c as ColorSlider.ColorSlider;
            if (slider != null)
            {
                slider.ThumbInnerColor = Color.FromArgb(99, 130, 208);
            }
        }
    }

or

foreach (Panel pnl in backPanel.Controls)
    {
        foreach (Control c in pnl.Controls)
        {
            if (c is ColorSlider.ColorSlider)
            {
                (c as ColorSlider.ColorSlider).ThumbInnerColor = Color.FromArgb(99, 130, 208);
            }
        }
    }

or

foreach (Panel pnl in backPanel.Controls)
    {
        foreach (Control c in pnl.Controls)
        {
            if (c is ColorSlider.ColorSlider)
            {
                ((ColorSlider.ColorSlider)c).ThumbInnerColor = Color.FromArgb(99, 130, 208);
            }
        }
    }

Upvotes: 2

Related Questions