因为刚学C#,想做个计算器,在做到加法运算时想点击“+”进行加法运算,可是想了很多办法都不行。主要是想在textbox1里输入完数字后,一点“+”号将textbox1里的数字传到textbox2中显示,然后继续在textbox1中输入,在textbox2中显示结果,有点累加的意思。就是不论textbox1里输入多少次始终在textbox2中累加结果,不知道有什么方法或是下面的代码应该怎么改。请教我一下:谢谢!(不知道用FOR循环行不行,太晚还没试.呵呵!)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace calculator
{
    public partial class Form1 : Form
    {
        public double T1_value;
        public double T2_value;
        public double N_num1 = 0;
        public double N_num2 = 0;
        public double Sum;
        
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text += "1"; ;
        }        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text += "2";
        }
        private void button3_Click_1(object sender, EventArgs e)
        {
            textBox1.Text += "3";
        }        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text += "4";
        }        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text += "5";
        }        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text += "6";
        }        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text += "7";
        }        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text += "8";
        }        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text += "9";
        }        private void button10_Click(object sender, EventArgs e)
        {
            textBox1.Text += "0";
        }        private void button11_Click(object sender, EventArgs e)//加法运算
        {                do
                {
                   if (textBox1.Text != "")
                        {
                            N_num1 = Convert.ToDouble(textBox1.Text);
                            N_num2 = Convert.ToDouble(textBox2.Text);
                            Sum = N_num1 + N_num2;
                            textBox2.Text = Sum.ToString();
                            textBox1.Text = "";
                           
                        }
 
                } while (textBox2.Text=="");
               
         
        }

解决方案 »

  1.   


    private void button11_Click(object sender, EventArgs e)//加法运算
            {         textBox2.Text =Convert.ToDouble(textBox2.Text ) +  Convert.ToDouble(textBox1.Text);                           
         textBox1.Text=“”;
            }
      

  2.   

    private void button11_Click(object sender, EventArgs e)//加法运算
            {
                       if (!string.IsNullOrEmpty(textBox1.Text))
                            {
                                if (double.TryParse(textBox1.Text, out N_num1) &&
                                double.TryParse(textBox2.Text, out N_num2))
                                {
                                  Sum = N_num1 + N_num2;
                                  textBox2.Text = Sum.ToString();
                                  textBox1.Text = "";
                                }
                               
                            }
            }
    大概看了一下,不知道你要的是不是这个意思.没开VS写,可能有点小错.
      

  3.   

    1楼和2楼两位的方法我都试了,还是不行,一楼的方式我用过,不过显示错误,在我的那段代码中一到这条语句就报错:N_num2 = Convert.ToDouble(textBox2.Text);好像是类型转换的时textbox2.Text没有赋值的问题。
      

  4.   

    private void button11_Click(object sender, EventArgs e)//加法运算
            {
     if(textBox2.Text.Equals(""))
             {
        textBox2.Text=0;
     }
     if(textBox1.Text.Equals(""))
             {
        textBox1.Text=0;
     }
             textBox2.Text =Convert.ToDouble(textBox2.Text ) +  Convert.ToDouble(textBox1.Text);                             textBox1.Text="";
             
            }
      

  5.   

    button11_Click内有
    textBox2.Text = Sum.ToString();
    textBox1.Text = "";
    程序有可能出现假死,小心了
      

  6.   

    button11_Click内有
    textBox1.Text += "1"; ;(这个地方是一个分号吧!)
    if (textBox1.Text != "")
    {
      N_num1 = Convert.ToDouble(textBox1.Text);
      N_num2 = Convert.ToDouble(textBox2.Text);
      Sum = N_num1 + N_num2;
      textBox2.Text = Sum.ToString(); (这个也在button11_Click,会有冲突出错吧)
     textBox1.Text = "";
    }
      

  7.   

    除错,加入异常判断.
    Convert.ToDouble(textBox2.Text);
    当转换失败时会抛出异常的.
    所以需要在前面判断是否能够转换为Double
      

  8.   

    textBox2.Text = Sum(ToString());
    textBox1.Text = "";
      

  9.   


    private void button11_Click(object sender, EventArgs e)//加法运算
    {            if (double.TryParse(TextBox1.text))
                {
                    if (double.TryParse(TextBox2.text))
                    {
                        TextBox2.text = double.Parse(TextBox1.text) + double.Parse(TextBox2.text);
                    }
                    else
                    {
                        TextBox2.text = double.Parse(TextBox1.text);
                    }
                } 
    }
      

  10.   

    用double.TryParse()判断比较好,这样就不会容易有异常
    private void button11_Click(object sender, EventArgs e)//加法运算
            {
                       if (!string.IsNullOrEmpty(textBox1.Text))
                            {
                                if (double.TryParse(textBox1.Text, out N_num1) &&
                                double.TryParse(textBox2.Text, out N_num2))
                                {
                                  Sum = N_num1 + N_num2;
                                  textBox2.Text = Sum.ToString();
                                  textBox1.Text = "";
                                }
                               
                            }
            }
      

  11.   

    已解决完的代码:不知道哪位老师看看能不能将代码再简便些
            private void button11_Click(object sender, EventArgs e)//加法运算
            {
                do
                {
                    if (textBox1.Text != "")
                    {
                        N_num1 = Convert.ToDouble(textBox1.Text);
                        if (N_num2 == 0)
                        {
                            N_num2 += N_num1;
                            textBox2.Text = N_num2.ToString();
                        }
                        else
                        {
                            N_num2 = Convert.ToDouble(textBox2.Text);
                            Sum = N_num1 + N_num2;
                            textBox2.Text = Sum.ToString();
                        }
                        textBox1.Text = "";
                    }
                    else
                    {
                        textBox1.Text = Convert.ToString("0");
                        textBox2.Text = Convert.ToString("0");
                        textBox1.Text = "";
                    }            } while (textBox2.Text == "");        }
      

  12.   

    你的+是进行的string类型操作 需要将string转换成int
      

  13.   

    新手路过。
    if (textBox1.Text =="")
                {
                    MessageBox.Show(" textBox1.Text is null!");
                }
                else
                { 
                    N_num1 = Convert.ToDouble(textBox1.Text);                if (textBox2.Text == "")
                    {
                        sum += N_num1;
                        textBox2.Text = sum.ToString(); 
                        textBox1.Text = "";
                    }
                    else
                    {
                        N_num2 = Convert.ToDouble(textBox2 .Text );
                        sum = N_num1 + N_num2;
                        textBox2.Text = sum.ToString();
                        textBox1.Text = "";
                    }
                }
      

  14.   

    private void button11_Click(object sender, EventArgs e)//加法运算
            {         textBox2.Text =Convert.ToDouble(textBox2.Text.Trim()==""?"0":textBox2.Text.Trim() ) +  Convert.ToDouble(textBox1.Text);                           
         textBox1.Text=“”;
            }
    记得判断textbox2超过double的有效长度没。用tryPrase