c# - 根据用户输入更改标签颜色

标签 c# .net

我正在尝试制作一个非常基本的程序。

我试图让用户在文本框中输入他们的温度,然后如果温度低于 15cold 将显示在标签中蓝色,如果 高于 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/

相关文章:

c# - 命名:解决方案、项目、命名空间和程序集

c# - 对 throw 表达式的说明

c# - 添加单例 Kafka 生产者 : attempted to read or write protected memory

.net - 从 .NET 类执行 QTP 自动化,而不启动 QTP 实例

c# - c#中的双倍乘法

C# .NET GetHashCode函数问题

.net - 绑定(bind)自动分离的情况

c# - 如何使用 ZeroMQ 解决 'NetMQ.AddressAlreadyInUseException'

c# - WPF 生成的文件无法编译

c# - windows窗体如何在文本框的右侧或左侧添加图标