代码不工作它不显示标签文本 [英] code not working it doesn't show the label text

查看:92
本文介绍了代码不工作它不显示标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

      private void button1_Click(object sender, EventArgs e)
      {
         int a, b, r; // to represent textboxes
         string Operation = textBox2.Text.ToString(); // to represent the operation

         {
            switch (Operation)
            {
               case "+": // operation to be perform

                  a = int.Parse(textBox1.Text); // to get the value of textbox1
                  b = int.Parse(textBox3.Text);  // to get the value of textbox3

                  if (String.IsNullOrEmpty(textBox1.Text)) // if user input nothing in textBox2
                     label6.Text = "Please enter the first number"; // label6 should appear this text.

                  if (String.IsNullOrEmpty(textBox3.Text)) // if user input nothing in textBox2
                     label8.Text = "Please enter the second number"; // label8 should appear this text.

                  r = a + b; //to get the difference of two textboxes.

                  textBox4.Text = r.ToString(); // to display the result in textbox4
                  break;

               case "-": // operation to be perform

                  a = int.Parse(textBox1.Text); // to get the value of textbox1
                  b = int.Parse(textBox3.Text);  // to get the value of textbox3

                   if (String.IsNullOrEmpty(textBox1.Text)) // if user input nothing in textBox2
                     label6.Text = "Please enter the first number"; // label6 should appear this text.

                  if (String.IsNullOrEmpty(textBox3.Text)) // if user input nothing in textBox2
                     label8.Text = "Please enter the second number"; // label8 should appear this text.

                  r = a - b; //to get the difference of two textboxes.

                  textBox4.Text = r.ToString(); // to display the result in textbox4
                  break;

               case "*": // operation to be perform

                  a = int.Parse(textBox1.Text); // to get the value of textbox1
                  b = int.Parse(textBox3.Text);  // to get the value of textbox3

                  if (String.IsNullOrEmpty(textBox1.Text)) // if user input nothing in textBox2
                     label6.Text = "Please enter the first number"; // label6 should appear this text.

                  if (String.IsNullOrEmpty(textBox3.Text)) // if user input nothing in textBox2
                     label8.Text = "Please enter the second number"; // label8 should appear this text.

                  r = a * b; //to get the product of two textboxes.

                  textBox4.Text = r.ToString(); // to display the result in textbox4
                  break;

               case "/": // operation to be perform

                  a = int.Parse(textBox1.Text); // to get the value of textbox1
                  b = int.Parse(textBox3.Text);  // to get the value of textbox3

                  if (String.IsNullOrEmpty(textBox1.Text)) // if user input nothing in textBox2
                     label6.Text = "Please enter the first number"; // label6 should appear this text.

                  if (String.IsNullOrEmpty(textBox3.Text)) // if user input nothing in textBox2
                     label8.Text = "Please enter the second number"; // label8 should appear this text.

                  if (textBox3.Text != "0")
                     textBox4.Text = "Impossible";

                  r = a / b; //to get the product of two textboxes.

                  textBox4.Text = r.ToString(); // to display the result in textbox4
                  break;

               default: // if the user input a value not in the operation.
                  textBox4.Text = "Cannot compute";
                  break;
            }
         }
      }
   }
}





分部也没有工作。 number / 0 devide by zero // formatexception未处理。输入字符串的格式正确。

当我添加/减去/乘以或将空白文本框分成不显示的数字标签文本时。



The division was not working either. number / 0 devide by zero //formatexception was unhandled. Input string was in a correct format.
When I add/subtract/multiply or divide blank textbox to a number label text not showing.

//format exception was unhandled. Input string was in a correct format on either one of;
 a = int.Parse(textBox1.Text); // to get the value of textbox1
 b = int.Parse(textBox3.Text);  // to get the value of textbox3





提前感谢您的帮助。

我只是想学习。



Thank you in advance for your help.
I just want to learn.

推荐答案

第一个要做的事情被称为验证你的输入 - 这是一种奇特的方式,说你的用户会犯错误。检查他给你的东西,并确保它是正确的 - 如果不是,告诉他,不要继续



它也很容易做到。我更喜欢s =在方法顶部进行所有验证,然后在出现问题时立即退出:

The first thing to do is called "validate your inputs" - which is a fancy way to say "your user will make mistakes. check what he gives you, and make sure it's right first - if it isn't, tell him and don't continue"

It's easy to do as well. I prefer to s=do all validation at teh top of the method, and then exit immediately if there is a problem:
private void button1_Click(object sender, EventArgs e)
   {
   int a;
   if (!int.TryParse(textBox1.Text, out a))
      {
      label6.Text = "Unrecognisable number";
      return;
      }
   ...

然后您可以使用 a 中的值而无需进一步检查或出现问题 - 所以也一样对于下一个输入,然后再也不要看文本框了。



试试吧 - 这并不难做,但它会使你的其余代码变得很多,很多更简单。



说到鸿沟,直接检查 b 中的值 - 并报告一个问题然后而不是分裂。



不使用Visual Studio的默认值也是一个非常好的主意,但是根据什么来命名你的控件他们这样做,而不是他们是什么。我将 textBox1 重命名为 tbFirstOperand textbox4 as tbResult - 它使您的代码更具可读性,并且在您返回代码后可以避免混淆。 (输入也更快,因为Intellisense会更快地填写它)

You can then use the value in a without further checks or problems - so do the same for the next input, then never look at the textboxes again.

Try it - it's not difficult to do, but it makes the rest of your code a lot, lot simpler.

When it comes to the divide, check the value in b against zero directly - and report a problem then instead of doing the division.

It's also a very, very good idea to not use the default values from Visual Studio, but to name your controls according to what they do, rather than what they are. I would rename textBox1 as tbFirstOperand and textbox4 as tbResult - it makes your code a lot more readable, and it saves confusion later when you come back to the code. (It's also quicker to type, since Intellisense will fill it in for you much sooner)


主要问题是你在检查之前得到(或试图获取)文本框中的整数值如果这些文本框实际上包含任何内容。



所以,例如,你可以从:

The main problem is you are getting (or trying to get) the integer values from your textboxes before checking if these textboxes actually contain anything.

So, for example, you could start with:
case "/":
   if (String.IsNullOrEmpty(textBox1.Text)) {
      label6.Text = "Please enter the first number";
      break;
   }
   if (String.IsNullOrEmpty(textBox3.Text)) {
      label8.Text = "Please enter the second number";
      break;
   }
   if (int.TryParse(textBox1.Text, out a) && int.TryParse(textBox3.Text, out b)) {
      if (b == 0) {
         textBox4.Text = "Division by zero is impossible."
         break;
      }
      r = a / b;
      textBox4.Text = r.ToString();
   }
   else {
      textBox4.Text = "Something was wrong with at least one of the values you provided";
   }
   break;





然后你可以将这个模板应用于其余三个案例(删除'检查b == 0'部分因为它与除了除法之外的东西无关。)



此外,你应该只评论不明显的东西。将字符串值影响到TextBox的Text属性是直截了当的,不需要注释。



Then you could apply this template with the three remaining cases (removing the 'check for b == 0' part as it is not relevant for something other than division).

Moreover, you should only comment what is not obvious. Affecting a string value to a TextBox's Text property is something straightforward that does not need to be commented.


这篇关于代码不工作它不显示标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆