我正在尝试制作一个非常基本的程序。
我试图让用户在文本框中输入他们的温度,然后如果温度低于 15,cold 将显示在标签中蓝色,如果 高于 15,它将以红色显示 hot。
到目前为止,这是我的代码:
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;
}
}
}
}
到目前为止,我的程序仅在 15 时以红色显示温度,在 14 时以蓝色显示温度,并将数字输出到标签,但目前没有其他数字对颜色有影响。
最佳答案
这满足了您的要求。 将 String(文本)转换为 Int(数字), 并比较值。
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;
}
}
关于c# - 根据用户输入更改标签颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19337171/