Windows Form color changing

Posted by Sef on Stack Overflow See other posts from Stack Overflow or by Sef
Published on 2010-04-15T15:16:55Z Indexed on 2010/04/15 15:33 UTC
Read the original article Hit count: 357

Filed under:

Hello,

So i am attempting to make a MasterMind program as sort of exercise.

  • Field of 40 picture boxes (line of 4, 10 rows)
  • 6 buttons (red, green, orange, yellow, blue, purple)

When i press one of these buttons (lets assume the red one) then a picture box turns red.
My question is how do i iterate trough all these picture boxes?
I can get it to work but only if i write :
And this is offcourse no way to write this, would take me countless of lines that contain basicly the same.

        private void picRood_Click(object sender, EventArgs e)
    {
        UpdateDisplay();
        pb1.BackColor = System.Drawing.Color.Red;
    }

Press the red button -> first picture box turns red
Press the blue button -> second picture box turns blue
Press the orange button -> third picture box turns orange
And so on...

Ive had a previous similar program that simulates a traffic light, there i could assign a value to each color (red 0, orange 1, green 2).
Is something similar needed or how exactly do i adress all those picture boxes and make them correspond to the proper button.

Best Regards.

© Stack Overflow or respective owner

Related posts about c#