本文介绍了标签不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 I wrote a code in c# windows form application contain many if statement when I run project and enter data the label should change its color according to the if clause, but the color is still as in the last label  





我尝试过:





What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            double height, weight;
            double BMI = 0;
            height = Convert.ToDouble(textBox1.Text);
            weight = Convert.ToDouble(textBox2.Text);


            BMI = weight / (height * height);
            if (BMI < 18.5)
            {
                label3.Text = Convert.ToString("Under Weight");
                label3.ForeColor = Color.Blue; }

            else if
                ((BMI > 18.5) && (BMI < 24.9))
            {    label3.Text = Convert.ToString("Normal");
                label3.ForeColor = Color.Black;
            }
           else if        ((BMI > 25) && (BMI < 29.9))

             {       label3.Text = Convert.ToString("Over Weight");
                     label3.BackColor = System.Drawing.Color.Green;
                     label3.ForeColor = Color.White;
            }

            else if (BMI >= 30)
                    label3.Text = Convert.ToString("Obese");
                label3.ForeColor = Color.Red;
        }

推荐答案

if (BMI < 18.5)
{
    // Under Weight
}
else if (BMI < 25)
{
    // Normal
}
else if (BMI < 30)
{
    // Over Weight
}
else
{
    // Obese
}


double height, weight;
double BMI = 0;
if (!double.TryParse(textBox1.Text, out height))
    {
    MessageBox.Show(



这篇关于标签不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 08:48