none
When pictureBox1.BackColor equals to black, I cannot change the backcolor of the other picture box already. RRS feed

  • Question

  • After I Click the green, the green picturebox will be highlighted by pictureBox1

    which is black in color. And after that I click red, blue, or yellow, they cannot

    be highlighted anymore. How to fix this error?

    public void Question1() { if(pictureBox1.BackColor == Color.Black) { pictureBox2.BackColor = Color.Transparent; pictureBox3.BackColor = Color.Transparent; pictureBox4.BackColor = Color.Transparent; } else if(pictureBox2.BackColor == Color.Black) { pictureBox1.BackColor = Color.Transparent; pictureBox3.BackColor = Color.Transparent; pictureBox4.BackColor = Color.Transparent; } else if(pictureBox3.BackColor == Color.Black) { pictureBox1.BackColor = Color.Transparent; pictureBox2.BackColor = Color.Transparent; pictureBox4.BackColor = Color.Transparent; } else if(pictureBox4.BackColor == Color.Black) { pictureBox1.BackColor = Color.Transparent; pictureBox2.BackColor = Color.Transparent; pictureBox3.BackColor = Color.Transparent; } } private void red_Click(object sender, EventArgs e) { getvalue(true, false, true); pictureBox4.BackColor = Color.Black; Question1(); } private void green_Click(object sender, EventArgs e) { getvalue(false, true, true); pictureBox1.BackColor = Color.Black; Question1(); } private void blue_Click(object sender, EventArgs e) { getvalue(false, true, true); pictureBox2.BackColor = Color.Black; Question1(); } private void yellow_Click(object sender, EventArgs e) { getvalue(false, true, true); pictureBox3.BackColor = Color.Black; Question1(); }


    Monday, November 13, 2017 2:29 AM

Answers

  • When you click on the green button, it sets pictureBox1 to black. When you click on another colour, pictureBox1 is still black so the "if" block in Question1 just does exactly what it did the first time.

    To fix it, rewrite Question one so that it just sets all pictureBoxes to transparent (no "if", just four lines). Then, in the button click events, call Question1 before setting the relevant pictureBox to black.

    • Marked as answer by Weishen0130 Monday, November 13, 2017 4:17 AM
    Monday, November 13, 2017 3:28 AM

All replies

  • When you click on the green button, it sets pictureBox1 to black. When you click on another colour, pictureBox1 is still black so the "if" block in Question1 just does exactly what it did the first time.

    To fix it, rewrite Question one so that it just sets all pictureBoxes to transparent (no "if", just four lines). Then, in the button click events, call Question1 before setting the relevant pictureBox to black.

    • Marked as answer by Weishen0130 Monday, November 13, 2017 4:17 AM
    Monday, November 13, 2017 3:28 AM
  • Thank you. Do you have any better method to do the highlight thing of the picture box?
    Monday, November 13, 2017 4:18 AM
  • No worries.

    There's always a better way, but without knowing the exact context it's hard to tell what to suggest. Perhaps drawing a border in a bright colour around the selection?

    Monday, November 13, 2017 5:07 AM