Cambiar el color de la etiqueta según la entrada del usuario

I am trying to make a very basic program.

I am trying to allow a user to enter their temperature into a text box and then if the temperature está por debajo de 15 la palabra frío will be displayed in a label in the color blue, if it está por encima de 15 it will be display picante in the color red.

So far this is my Code:

namespace FinalTemperature
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
            double addTemperature;
            addTemperature = Double.Parse(txttemp.Text);
            lblans.Text = addTemperature.ToString();

            if (txttemp.Text.Trim() == "15")
            {
                lblans.ForeColor = System.Drawing.Color.Red;
            }

            if (txttemp.Text.Trim() == "14") 
            {
                lblans.ForeColor = System.Drawing.Color.Blue;
            }
        }
    }
}

So far my program just displays the temperature in red if its 15 and blue if its 14 and just outputs the number to the label, but at the moment no other numbers have an effect on the color.

preguntado Oct 12 '13, 18:10

Why vote his question down, its a legitimate question, especially for someone who is learning how to program. -

@DavidYancey I think someone must have downvoted over a cursory read and not realized what the OP was asking, which is why I've made some keywords bold. I can say this because, mods, especially who look at the review queue see so so many horrendous questions everyday that sometimes they can be brutal. -

@user Upvotes added to counteract downvotes. Sorry your question was downvoted (not by me) it may have just been a misunderstanding by moderators who see far too many crappy questions. -

@MystereMan SO does not have an issue with asking homework questions, assuming certain criteria are met: meta.stackexchange.com/questions/10811/… -

@Veleous - Where did I say there was anything wrong. I was just asking if it was homework. If it's homework, I treat answers differently than I do other kinds of answers, because I prefer to give hints to allow the asker to reach the conclusion themselves. Also, if it's homework, we should add the homework tag. -

6 Respuestas

This answers your requirements. Convert the String (text) to Int (numbers), and compare the values.

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Int32.Parse(txttemp.Text.Trim()) >= 15)
        {
            lblans.Text = "Hot";
            lblans.ForeColor = System.Drawing.Color.Red;
        } 
        else 
        {
            lblans.Text = "Cold";
            lblans.ForeColor = System.Drawing.Color.Blue;
        }
    }

Respondido 12 Oct 13, 19:10

You're actually checking equality to "15" and "14" ignoring all other values.

Prueba esta

if(addTemperature < 15)
{
    lblans.ForeColor = System.Drawing.Color.Blue;
}
else
{
    lblans.ForeColor = System.Drawing.Color.Red;
}

Respondido 12 Oct 13, 18:10

ASPX

Add a label in page,

<asp:label id="lbl" runat="server"></asp:label>, 

En su código detrás:

// Use the condition given below:

if(Convert.ToInt32(txtTemp.Text)>15 )
{
  lbl.Text= "HOT";
  lbl.ForeColor= System.Drawing.Color.Red;
}
else
{
    lbl.Text="COLD";
         lbl.ForeColor= System.Drawing.Color.Blue;
}

The above code converts the string value to Int32, so that you can compare it with any numeric value you want, instead of taking the compared value as string. You cannot apply logical operator other than equals to , to string.

Respondido 12 Oct 13, 19:10

the OP has not mentioned that he is working with ASP framework - pequeño

@Liel: Did you see the second line of the code, he had given. - user240141

lblans.ForeColor = addTemperature < 15 ? System.Drawing.Color.Blue :  System.Drawing.Color.Red;

Respondido 12 Oct 13, 18:10

Necesitas usar el <=, >=, >o < operadores, used for checking if numbers are less than, more than, etc. You will also need to use the converted double for this.

// If LESS than 15
if (addTemperature < 15)
{
    lblans.ForeColor = System.Drawing.Color.Blue;
}
// Else, meaning anything other is MORE than 15, you could also do:
// else if (addTemperature >= 15), but that wouldn't be needed in this case
else
{
    lblans.ForeColor = System.Drawing.Color.Red;
}

The reason your previous code did not work was because you were only checking for 15 and 14, and not any other values.

As I was reading your question I also thought a operador ternario would be a perfect use for this.

 lblans.ForeColor = addTemperature < 15 ? System.Drawing.Color.Blue : System.Drawing.Color.Red

Respondido 12 Oct 13, 18:10

You are testing for 14 and 15 specifically. You'll need to convert the strings to ints and compare with >= and <=.

Respondido 12 Oct 13, 19:10

@user2864740, whatever! ;) - david arno

@DavidArno - Casting and Converting are two very different things. - Erik Funkenbusch

@Mystere No, they are not. Please check section 7.7.6 of the C# spec. I accept that I was being sloppy in use the word cast, as strings cannot be cast to ints as C# defines the term, but casts are a subset of all the conversions C# supports. - david arno

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.