简体   繁体   中英

Iterate through visual objects

I am having a problem in Visual Studio 2013. I have like 72 picture boxes. And I have a variable named code which takes a random number like 13. Now I want to change pictureBox 13 's background color. But I don't want to make a long switch case for it. I just want something like a

for(int i=0;i<73;i++)
    pictureBox + code.BackColor = Color.Brown;

Is this possible?

No loop needed (at least no loop written by you) but a single line could return the required box

string name = "pictureBox" + randomNumber.ToString();
PictureBox p = this.Controls.OfType<PictureBox>()
                            .FirstOrDefault(x => x.Name == name)
p.BackColor = yourColor;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM