【问题标题】:Create a Windows application that contains two textboxes and two buttons创建一个包含两个文本框和两个按钮的 Windows 应用程序
【发布时间】:2015-08-15 02:36:30
【问题描述】:

当我尝试将字体颜色设置为黄色时,我搞砸了我的代码。请帮忙!!

说明: 创建一个包含两个文本框和两个按钮的 Windows 应用程序。文本框应该用于允许用户输入两个正数值。这些按钮应标记为“加”和“乘”。创建检索值、执行计算并在标签上显示计算结果的事件处理程序方法。结果标签最初应设置为不可见,字体颜色为黄色。如果输入了无效数据,请将结果标签上的字体颜色更改为红色并显示消息“值必须为数字且 >0”。显示最终答案时,字体颜色应为黄色。文本框标题需要额外的标签。不允许输入非数字字符。调用 TryParse() 方法来检索值。程序语句中涉及的所有控件都应命名。右对齐文本框中的值。

这是我在 form1 页面上得到的内容

namespace Add_and_Multiply
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int val1;
            int val2;
            val1 = Convert.ToInt32(textBox1.Text);
            val2 = Convert.ToInt32(textBox2.Text);
            label3.Text = Convert.ToString(val1 + val2); //after this line I tried putting the code to give me yellow color text and now I have so many errors:(
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int val1;
            int val2;
            val1 = Convert.ToInt32(textBox1.Text);
            val2 = Convert.ToInt32(textBox2.Text);
            label3.Text = Convert.ToString(val1 * val2);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

谁能帮帮我?我不太擅长这个。 谢谢

有人告诉我要这样修复它。

namespace Add_and_Multiply
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int val1;
            int val2;
            if(!int.TryParse(textBox1.Text.Trim(), out val1))
            {
                lblYellow.ForeColor = Color.Yellow;
                lblYellow.Visible = true;
                lblYellow.Text = "First value is invalid";
                return;
            }
            if(!int.TryParse(textBox2.Text.Trim(), out val2))
            {
                lblYellow.ForeColor = Color.Yellow;
                lblYellow.Visible = true;
                lblYellow.Text = "Second value is invalid";
                return;
            }
            lblYellow.Visible = false;
            label3.Text = Convert.ToString(val1 + val2);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int val1;
            int val2;
            if(!int.TryParse(textBox1.Text.Trim(), out val1))
            {
                lblYellow.ForeColor = Color.Yellow;
                lblYellow.Visible = true;
                lblYellow.Text = "First value is invalid";
                return;
            }
            if(!int.TryParse(textBox2.Text.Trim(), out val2))
            {
                lblYellow.ForeColor = Color.Yellow;
                lblYellow.Visible = true;
                lblYellow.Text = "Second value is invalid";
                return;
            }
            lblYellow.Visible = false;
            label3.Text = Convert.ToString(val1 * val2);


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

但我收到一个错误“名称 'lblYellow' 在当前上下文 Form1.cs 中不存在”我应该在 private void button1_click 行之前声明它吗?

【问题讨论】:

  • 也许你应该清楚这是否是一个家庭作业问题。
  • 是的,很抱歉不清楚。

标签: c# windows-applications eventhandler tryparse


【解决方案1】:

你有什么错误?,我认为你必须在行后删除一个}:

label3.Text = Convert.ToString(val1 + val2);

【讨论】:

  • “预期的类、委托、枚举、接口或结构” - 它表示与 private void button2_Click 和 private void Form1_Load 一致
  • 第二个错误是“类型或命名空间定义,或预期的文件结尾”它在最后一行
【解决方案2】:
label3.ForeColor = Color.Yellow;

...应该将标签颜色更改为黄色...

但是你还有另一个问题。您没有检查无效输入以将标签更改为红色,也没有调用 TryParse 作为指令命令。

此外,说明指出用户应该能够输入任何正数值。这将包括 8.9216.75。我想如果你想要最好的成绩,你需要考虑到这一点。看看C# built-in data types

【讨论】:

  • 错误现在消失了,但现在它突出显示前景色作为错误。我在 label3.Text = Convert.ToString(val1 + val2); 之后添加了您建议的代码
  • 错误,是的,现在应该修复了。 ForeColor 而不是 ForegroundColor。
  • 哦,有帮助!谢谢!它是如此明亮,但我希望这就是它应该做的事情。现在我只需要弄清楚如何处理无效数据部分
【解决方案3】:

看起来您正在定义一个没有类或在类外的方法,请检查所有方法是否在 Form1 内,并检查是否有额外或缺少的括号。

【讨论】:

    【解决方案4】:

    如果有人对这个项目有疑问,那么这里就是最终产品。运行后看起来还不错。

    一切都很好,它应该如何!


    { 公共部分类Form1:表格 { 公共表格1() { 初始化组件(); }

        private void button1_Click(object sender, EventArgs e)
        {
            int val1;
            int val2;
            int valid1 = 0;
            int valid2 = 0;
            if(int.TryParse(textBox1.Text.Trim(), out val1))
            {
                if(val1>0)
                {
                    valid1 = 1;
                    caption1.ForeColor = Color.Yellow;
                    caption1.Visible = true;
                    caption1.Text = "First value is valid"; 
                }
                else
                {
                    caption1.ForeColor = Color.Red;
                    caption1.Visible = true;
                    caption1.Text = "Value must be Greater than 0";
                }
            }
            else
            {
                caption1.ForeColor = Color.Red;
                caption1.Visible = true;
                caption1.Text = "Value must be Numeric";
            }
            if(int.TryParse(textBox2.Text.Trim(), out val2))
            {
                if (val2 > 0)
                {
                    valid2 = 1;
                    caption2.ForeColor = Color.Yellow;
                    caption2.Visible = true;
                    caption2.Text = "First value is valid";
                }
                else
                {
                    caption2.ForeColor = Color.Red;
                    caption2.Visible = true;
                    caption2.Text = "Value must be Greater than 0";
                }
            }
            else
            {
                caption2.ForeColor = Color.Red;
                caption2.Visible = true;
                caption2.Text = "Value must be Numeric";
            }
            if((valid1 > 0)&& (valid2 > 0))
            {
                label3.Visible = true;
                label3.ForeColor = Color.Yellow;
                label3.Text = Convert.ToString(val1 + val2);
            }
            else
            {
                label3.Visible = false;
            }
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            int val1;
            int val2;
            int valid1 = 0;
            int valid2 = 0;
            if(int.TryParse(textBox1.Text.Trim(), out val1))
            {
                if (val1 > 0)
                {
                    valid1 = 1;
                    caption1.ForeColor = Color.Yellow;
                    caption1.Visible = true;
                    caption1.Text = "First value is valid";
                }
                else
                {
                    caption1.ForeColor = Color.Red;
                    caption1.Visible = true;
                    caption1.Text = "Value must be Greater than 0";
                }
            }
            else
            {
                caption1.ForeColor = Color.Red;
                caption1.Visible = true;
                caption1.Text = "Value must be Numeric";
            }
            if(int.TryParse(textBox2.Text.Trim(), out val2))
            {
                if (val2 > 0)
                {
                    valid2 = 1;
                    caption2.ForeColor = Color.Yellow;
                    caption2.Visible = true;
                    caption2.Text = "First value is valid";
                }
                else
                {
                    caption2.ForeColor = Color.Red;
                    caption2.Visible = true;
                    caption2.Text = "Value must be Greater than 0";
                }
            }
            else
            {
                caption2.ForeColor = Color.Red;
                caption2.Visible = true;
                caption2.Text = "Value must be Numeric";
    
            }
            if ((valid1 > 0) && (valid2 > 0))
            {
                label3.Visible = true;
                label3.ForeColor = Color.Yellow;
                label3.Text = Convert.ToString(val1 * val2);
            }
            else 
            {
                label3.Visible = false;
            }
             }
    
        private void Form1_Load(object sender, EventArgs e)
        {
    
        }
    
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
    
        }
    }
    

    }

    【讨论】:

      【解决方案5】:

      我知道这是两年后的事了,但我认为人们可能仍在寻求帮助。

      private void btnAdd_Click(object sender, EventArgs e)
      {
          int firstNumber, secondNumber;
      
          if (!int.TryParse(txtBxFirstNumber.Text, out firstNumber) || firstNumber < 1)
          {
              lblResults.Text = "First number needs\n to be numeric and > 0";
              lblResults.ForeColor = Color.Red;
              lblResults.Visible = true;
              txtBxFirstNumber.Clear();
              return;
          }
          if (!int.TryParse(txtBxSecondNumber.Text, out secondNumber) || secondNumber < 1)
          {
              lblResults.Text = "Second number need\n to be numeric and > 0";
              lblResults.ForeColor = Color.Red;
              lblResults.Visible = true;
              txtBxSecondNumber.Clear();
              return;
          }
          lblResults.ForeColor = Color.Yellow;
          lblResults.Visible = true;
          lblResults.Text = Convert.ToString(firstNumber + secondNumber);
      }
      
      private void btnMultiply_Click(object sender, EventArgs e)
      {
          int firstNumber, secondNumber;
      
          if (!int.TryParse(txtBxFirstNumber.Text, out firstNumber) || firstNumber < 1)
          {
              lblResults.Text = "First number needs\n to be numeric";
              lblResults.ForeColor = Color.Red;
              lblResults.Visible = true;
              txtBxFirstNumber.Clear();
              return;
          }
          if (!int.TryParse(txtBxSecondNumber.Text, out secondNumber) && secondNumber < 1)
          {
              lblResults.Text = "Second number need\n to be numeric";
              lblResults.ForeColor = Color.Red;
              lblResults.Visible = true;
              txtBxSecondNumber.Clear();
              return;
          }
          lblResults.ForeColor = Color.Yellow;
          lblResults.Visible = true;
          lblResults.Text = Convert.ToString(firstNumber * secondNumber);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-19
        • 2019-09-23
        • 1970-01-01
        • 2020-12-20
        • 1970-01-01
        相关资源
        最近更新 更多