唧唧改变C#中复选框的背景颜色? [英] Haw to change the background color of the checkbox in C#?

查看:507
本文介绍了唧唧改变C#中复选框的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个答案的问题的表单。如果我检查了正确的答案,背景会变为绿色,但如果我错了,背景颜色会变为红色。但如果我检查,之后我取消选中颜色是透明的。请Hellp



我尝试了什么:



i试用

 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;
}



但是效果不好

解决方案

不太确定你要做什么,但逻辑是错误的。



代码仅在

时执行(如果) checkBox1.Checked == true&& checkBox2.Checked == false)



但是你有:

 if(checkBox2.Checked == true)



这绝不会发生。

......和

(checkBox1.Checked == true)


总是发生


< blockquote>听起来你应该使用单选按钮而不是复选框。



单选按钮是互斥的。



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





如果你坚持使用复选框,你需要知道哪一个点击后需要取消选中它才能在点击事件中保留它。



最好为每个单选按钮分别单击事件以取消选中其他按钮


 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;

}
其他
{
cbWrong.BackColor = System.Drawing.Color.Transparent;
}

}


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

解决方案

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!


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;
    }

}


这篇关于唧唧改变C#中复选框的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆