Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a form with a question with 2 answers. And if i checked the right answer the background is changing in green, but if i wrong the background color is changing in red. But if i checked and after that i unchecked the the color is channging in transparent. Please Hellp

What I have tried:

i tried with
private void button1_Click(object sender, EventArgs e)
       {
           if (checkBox1.Checked == true && checkBox2.Checked == false)
           {
               if (checkBox1.Checked == true)
                   checkBox1.BackColor = System.Drawing.Color.Green;
               else checkBox1.BackColor = System.Drawing.Color.Transparent;
               if (checkBox2.Checked == true)
                   checkBox2.BackColor = System.Drawing.Color.Red;
               else checkBox2.BackColor = System.Drawing.Color.Transparent;
           }

but isn't workin good
Posted
Updated 17-May-17 9:59am

Not quite sure what you're trying to do, but the logic is wrong.

Code only executes when
if (checkBox1.Checked == true && checkBox2.Checked == false)

but then you have:
if (checkBox2.Checked == true)

Which can never happen.
...and
(checkBox1.Checked == true)

which always happens!
 
Share this answer
 
v2
sounds like you should be using radio buttons not checkboxes.

Radio buttons are mutually exclusive.

private void radioButton_CheckedChanged(object sender, EventArgs e)
 {
     if (this.radioButton1.Checked)
     {
         this.radioButton1.BackColor = System.Drawing.Color.Green;
     }
     else
     {
         this.radioButton1.BackColor = System.Drawing.Color.Transparent;
     }
     if (this.radioButton2.Checked)
     {
         this.radioButton2.BackColor = System.Drawing.Color.Red;
     }
     else
     {
         this.radioButton2.BackColor = System.Drawing.Color.Transparent;
     }
 }


if you insist on using checkboxes you will need to know which one was clicked and will need to uncheck it first keeping this in the click event.

maybe best to have separate click events for each radio button to uncheck the other

private void cbCorrect_Click(object sender, EventArgs e)
{
    if (this.cbCorrect.Checked)
    {
        this.cbCorrect.BackColor = System.Drawing.Color.Green;

        this.cbWrong.Checked = false;
        this.cbWrong.BackColor = System.Drawing.Color.Transparent;

    }
    else
    {
        cbCorrect.BackColor = System.Drawing.Color.Transparent;
    }
}

private void cbWrong_Click(object sender, EventArgs e)
{
    if (this.cbWrong.Checked)
    {
        this.cbWrong.BackColor = System.Drawing.Color.Red;

        this.cbCorrect.Checked = false;
        this.cbCorrect.BackColor = System.Drawing.Color.Transparent;

    }
    else
    {
        cbWrong.BackColor = System.Drawing.Color.Transparent;
    }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900