无法在文本框中显示答案 [英] Cant get the answer to show in a text box

查看:63
本文介绍了无法在文本框中显示答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我正在创建一个窗口表单,要求您输入12个数字,然后在文本框中显示它们。我做到了。但它要求将数组中的所有数字添加到一个总计框中,以及一个增值税框,最小值和最大值。我似乎无法得到答案显示在总框中。当按下计算按钮时,我似乎已经显示数字0。但现在没有任何显示。以下是所有代码,以及如何提供帮助。



Basically im creating a windows form that asks you to enter 12 numbers and then display them in a text box. Ive done that. But it asks to add all the numbers in the array to be summed up in a total box, as wel as a vat box, min and max. I cannot seem to get the answer to display in the total box. I seemed to have got the number 0 to show when the Calculate button was pressed.But now nothing shows. Here is all the code, how you can help.

public partial class Form1 : Form
    {
        string[] _prices = new string[2];
        int _index;
        string _outputDouble = "";

        double _total = 0.0;

        public void ConvertStringToDouble()
        {
            double[] _prices = new double[_index];

            for (_index = 0; _index < 2; _index++)
            {
                _prices[_index] = double.Parse(_outputDouble);
            }
        }

        private void Total()
        {
            double[] _prices = new double [_index];
            _total = 0.0;

            foreach (double price in _prices)
            {
                _total = _total + price;
            }

    //        _total =

            totalDisplay.Text = _total.ToString();

        }

        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void enterPriceButton_Click(object sender, EventArgs e)
        {
            if (_index < 2)
            {
                _prices[_index] = priceTextBox.Text;
                priceTextBox.Text = "";
                _index++;
            }
            else
            {
                enterPriceButton.Enabled = false;
                Display();
            }
      }


        private void Display()
        {
            _outputDouble = "";

            foreach (string price in _prices)
            {
                _outputDouble = _outputDouble + price + "\r\n";
            }

            displayTextBox.Text = _outputDouble;

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void displayTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void totalDisplay_Click(object sender, EventArgs e)
        {

        }

        private void calcTotalButton_Click(object sender, EventArgs e)
        {
            calcTotalButton.Enabled = false;
            Total();
        }
    }

推荐答案

这里有重大问题,第一个问题似乎是你有不了解变量及其范围 - 这不是一个主要问题,因为它非常简单。



让我们暂时忽略计算机并考虑朋友。假设你有一个叫Bob的朋友,他来看你 - 所以你把他放在起居室里,然后去给他喝杯茶。不幸的是,鲍勃是聋人,所以当他在同一个房间时他只能听到你 - 因为他嘴唇读。当你在厨房时,鲍勃看不到你,所以他无法向你传达任何信息 - 例如他想要的糖的数量。他的范围仅限于起居室,一旦你离开,你就无法与他交谈。所以,你回到起居室问他糖,并惊喜!这是一个不同的鲍勃谁对茶一无所知!



这里发生的事情是夸张的,但是方法会发生这种情况:你在方法中声明的任何变量都只是可访问且仅在执行方法时存在。可怜的鲍勃 - 当你去喝茶的时候你就杀了他,当你回来吃糖的时候你创造了一个新的...



如果你想让Bob成为坚持不懈,然后他需要有一个覆盖整个房子的范围 - 他需要让他的听力恢复。这意味着在类级别而不是在方法中声明Bob变量:



There are significant problems here, the first of which seems to be that you have no understanding of variables and their scope - that's not a major problem as it's pretty simple really.

Let's ignore computers for a moment or two and think about friends. Suppose you have a friend called Bob, and he comes round to see you - so you sit him down in the living room, and go to get him a cup of tea. Unfortunately, Bob is deaf, so he can only "hear" you when he is in the same room - because he lip reads. As soon as you are in the kitchen, Bob can't see you, so he can't communicate any information to you - such as the number of sugars he would like. His scope is limited to the living room and as soon as you leave it you can't talk to him. So, you go back to the living room to ask him about the sugar, and surprise! It's a different Bob who knows nothing about the tea!

What's happened here is exaggerated, but it is what happens with methods: any variables you declare within the method are only accessible and only exist while the method is being executed. Poor Bob - you killed him when you went to get the tea, and created a new one when you came back for sugar...

If you want Bob to be persistent, then he needs to have a scope which covers the whole house - he needs to get his hearing back. This means declaring the Bob Variable at a class level instead of in a method:

class House
   {
   MyFriend Bob = new MyFriend();
   void LivingRoom()
      {
      Bob = MyFriend.WantsTea;
      }

   void Kitchen()
      {
      if (makingTea)
         {
         int sugars = Bob.SugarCount;
         ...
         }
      }
   }



因为Bob是在任何方法之外声明的,所以他的范围是整体House类的任何例程 - House类中的任何例程都可以访问他(除非你在方法中声明一个名为Bob的新变量,在这种情况下变量优先)。



所以,看看你的代码,看看Bob(AKAK _prices)在每种情况下都有哪个范围!


Because Bob is declared outside any method, his scope is the whole of the House class - and any routine in the House class can access him (unless you declare a new variable called Bob inside a method in which case that variable takes precedence).

So, look at your code, and see which scope Bob (AKAK _prices) has in each case!


我删除了不需要的代码并确保了适当的变量范围。我没有添加代码来验证文本框中的数据实际上是数字,并且可以成功转换为 double 数据类型。另外,我没有添加代码来限制条目数为12。一个完整的应用程序可以做这些事情。





在C#Visual Studio 2012中测试:



I removed code that was not needed and ensured proper scope of variables. I did not add code to validate that the data in the textbox was actually numeric and could be successfully converted to a double data type. Also, I did not add code to limit the number of entries to twelve. A complete application would do those things.


Tested in C# Visual Studio 2012:

double[] _prices = new double[11];     // Declare room for 12 double items
int _index=0;                          // 0-11 - Which index of the 12 items?

 private void Total()
 {
     double _total = 0.0;
     foreach (double price in _prices)
     {
         _total = _total + price;
     }
     totalDisplay.Text = _total.ToString("###,###,###,###.00");
 }

 private void enterPriceButton_Click(object sender, EventArgs e)
 {
     _prices[_index] = double.Parse(priceTextBox.Text);
    priceTextBox.Text = "";
    // Add the latest price to the end of the textbox
    displayTextBox.Text += _prices[_index].ToString("###,###,###,###.00") + "\r\n";
    _index++;
}

 private void calcTotalButton_Click(object sender, EventArgs e)
 {
     calcTotalButton.Enabled = false;
     Total();
 }


这篇关于无法在文本框中显示答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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