复选框if语句问题 [英] Checkbox if-statement problem

查看:118
本文介绍了复选框if语句问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (sexch.Checked == true)
            {

                ch.Text = "male";
            }
           else  if (sexch.Checked == false)
            {
                ch.Text = "Female";

            }





当复选框==时我有这个代码我想要更改label.text对男性问题是该程序只读取其他内容。



我从数据库中获取sexch复选框,并根据数据库的输出(true = male,false = female)我要更改label.text 。



我的尝试:





I have this code when the checkbox ==true I want a label.text to change to male. The problem is that the program only read the else.

I am getting the sexch checkbox from a database and depending on the output(true=male, false=female) of the database I want to change the label.text.

What I have tried:

if (sexch.Checked == true)
            {

                ch.Text = "male";
            }
           else  if (sexch.Checked == false)
            {
                ch.Text = "Female";

            }










if (sexch.Checked == true)
            {

                ch.Text = "male";
            }
             if (sexch.Checked == false)
            {
                ch.Text = "Female";

            }

推荐答案

您的问题是您根本不需要第二个if语句。如果复选框不为true,则必须为false。

Your problem is that you don't need the second if statement at all. If the checkbox is not true, it must be false.
if (sexch.Checked == true)
{
    ch.Text = "Male";
}
else
{
    ch.Text = "Female";
}


使用 CheckBox.CheckedChanged事件(System.Windows.Forms) [ ^ ]



在其中,使用:

Use CheckBox.CheckedChanged Event (System.Windows.Forms)[^]

Inside of it, use:
ch.Text = sexch.Checked ? "Male" : "Female";





如果您想根据数据库值更改复选框状态:



In case you want to change checkbox state based on database value:

sexch.Checked = database_value;







详情请见: CheckBox.Checked属性(System.Windows.Forms) [ ^ ]


这篇关于复选框if语句问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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