Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
 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:

C#
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;
        }
Posted
Updated 13-May-19 20:52pm
v2

I think your if clauses don't match below BMI value.
- BMI == 18.5
- 24.9 <= BMI && BMI <= 25
- 29.9 <= BMI && BMI < 30

Below code will be match any BMI value.
C#
if (BMI < 18.5)
{
    // Under Weight
}
else if (BMI < 25)
{
    // Normal
}
else if (BMI < 30)
{
    // Over Weight
}
else
{
    // Obese
}
 
Share this answer
 
v2
For starters, don't use Convert methods with user input - if the value they enter doesn't match the right format, your application will crash. Use the TryParse methods instead:
C#
double height, weight;
double BMI = 0;
if (!double.TryParse(textBox1.Text, out height))
    {
    MessageBox.Show($"\"{textBox1.Text}\" is not a valid height");
    return;
    }
if (!double.TryParse(textBox2.Text, out weight))
    {
    MessageBox.Show($"\"{textBox3.Text}\" is not a valid weight");
    return;
    }
Secondly, you are missing cases where values are equal: 18.5, between 24.9 and 25, between 29.9 and just under 30.
Change your tests to these, and you cover all cases (as well as making your code more readable):
C#
            if (BMI < 18.5)
                {
...
                }
            else if (BMI < 25.0)
                {
...
                }
            else if (BMI < 30.0)
                {
...
                }
Thirdly, especially when you are getting started, always use curly brackets even if they aren't needed, that way your code is more obvious:
C#
else if (BMI >= 30)
        label3.Text = Convert.ToString(" obese");
    label3.forecolor="Color.Red;
Is the equivalent of:
C#
else if (BMI >= 30)
        {
        label3.Text = Convert.ToString(" obese");
        }
    label3.forecolor="Color.Red;
That's not the same as this:
C#
else if (BMI >=30)
    {
    label3.Text = Convert.ToString("Obese");
    label3.ForeColor = Color.Red;
    }
And it means that the ForColor is set to Red every time!
You also have no method for resetting a colour after someone is overweight - so when you set the BackColor to Green, it never returns to white again.
A better way to do this is like this:
C#
BMI = weight / (height * height);
string text = "Major Error!";
Color backColor = SystemColors.Window;
Color foreColor = SystemColors.WindowText;
if (BMI < 18.5)
    {
    text = "Under Weight";
    foreColor = Color.Blue;
    }
else if (BMI < 25.0)
    {
    text = "Normal";
    foreColor = Color.Black;
    }
else if (BMI < 30.0)
    {
    text = "Over Weight";
    backColor = System.Drawing.Color.Green;
    foreColor = Color.White;
    }
else
    {
    text = Convert.ToString("Obese");
    foreColor = Color.Red;
    }
label3.Text = text;
label3.ForeColor = foreColor;
label3.BackColor = backColor;
Note that I got rid of your spurious Convert.ToString calls as well - you don't need to convert a string to a string, that's just silly, and makes your code harder to read.

Finally, do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
v2
Untested, since I don't have VS here, but you might try Label3.Refresh(); or Label3.Update(); I'm pretty sure one of those should cause the redraw with the correct color.

(Also, change your code to what Member 13432091 said. Much cleaner.)
 
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