Your code is wrong in so many fronts.
As it is, what you're doing is creating 25 strings with values panel0
, panel1
, panel2
etc., and trying to assign a value to a property of it. But strings don't contain a property named Visible
, so obviously you'll get an error.
What you want to do is get hold of controls of type Panel
in your form, and set their values.
foreach(var panel in this.Controls.OfType<Panel>())
{
panel.Visible = true;
}
Caveat: the above will only find Panel
controls in your topmost form. If there are controls that are nested, you'd want to perhaps write a method to recursively find them. Above is just to give you the idea.
In addition, if you have multiple Panel
controls and if you only want to set the property of those panels names fit your naming convention you can filter them out.
foreach(var panel in this.Controls.OfType<Panel>())
{
if( panel name fits your naming convention)
panel.Visible = true;
}
Here, you can look for correct panel name by using a Regex
, use a custom function etc.