Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Windows Forms- How to change a single property of more than one control with the same parent?

Is there any way I can make it so a certain property of several controls can be changed using say, hypothetically, a FOR loop? All these controls I am mentioning have the same parent which is another panel. I’ve tried searching for answers but I can’t find them or some of them talk about other things.

private void InitiateLargeDialogue(string title, string text1, string text2, Size size)
        {
            lblLargeTitle.Text = title;
            lblLargeText1.Text = text1;
            lblLargeText2.Text = text2;

            lblLargeText3.Visible = false; 
            lblLargeText4.Visible = false; 
            lblLargeText5.Visible = false; 
            pnlLargeExample.Visible = false;

            btnYes.Visible = false; 
            btnNo.Visible = false;
        }

Thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Honestly what you have now is what I’d expect of a WinForms project — it’s instantly readable.

However, if you’d like, you can do the following instead:

Control[] controlarray = new Control[]
{
    lblLargeText3,
    lblLargeText4,
    lblLargeText5,
    pnlLargeExample,
    btnYes,
    btnNo
}
private void InitiateLargeDialogue(string title, string text1, string text2, Size size)
{
    lblLargeTitle.Text = title;
    lblLargeText1.Text = text1;
    lblLargeText2.Text = text2;

    foreach(Control c in controlarray)
        c.Visible = false;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading