老师让做个简易计算器,我纠结了,做好按钮后,用尽了办法都没搞定,希望大伙儿能帮助我,在你们的百忙之中,我是新手,没多少分,希望能得到大家的帮忙
计算器的结构很简单,一个文本框,(0~9,+ - * / = 清除) 十六个按钮

解决方案 »

  1.   

    这是原先我做的,你看一下吧
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace Proj8_1
    {
        public partial class Form1 : Form
        {
            private string s;//定义变量s用于获得按钮名
            private double x, y;//定义变量x,y用于表示两运算数
            private Button btn;//定义按钮变量
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.Text = "";
                label1.Text = "";
            }        private void buttond_Click(object sender, EventArgs e)
            //单击数字命令按钮的事件处理程序
            {
                btn = (Button)sender;
                textBox1.Text = textBox1.Text + btn.Text;
            }        private void buttonop_Click(object sender, EventArgs e)
                //单击运算符命令按钮的事件处理程序
            {
                btn = (Button)sender;
                if (btn.Name != "button12")
                {
                    x = Convert.ToDouble(textBox1.Text);
                    textBox1.Text = "";
                    s = btn.Name;
                    label1.Text = x.ToString();
                }
                else
                {
                    if (label1.Text == "")
                        MessageBox.Show("输入不正确!!!", "信息提示", MessageBoxButtons.OK);
                    else
                    {
                         y = Convert.ToDouble(textBox1.Text);
                         switch (s)
                        {
                            case "button13":
                                textBox1.Text = (x + y).ToString();
                                break;
                            case "button14":
                                textBox1.Text = (x - y).ToString();
                                break;
                            case "button15":
                                textBox1.Text = (x * y).ToString();
                                break;
                            case "button16":
                                if (y == 0)
                                    MessageBox.Show("除零错误!!!", "信息提示", MessageBoxButtons.OK);
                                else
                                    textBox1.Text = (x / y).ToString();
                                break;
                        }
                        label1.Text = textBox1.Text;
                    }
                }
            }        private void button17_Click(object sender, EventArgs e)
            {
                btn = (Button)sender;
                textBox1.Text = "";
                label1.Text = textBox1.Text;
            }   }
    }
      

  2.   

    flashyyp123给的代码不错 我大概知道了怎么去写了
     希望能和大家交个朋友 
      

  3.   

      private void buttond_Click(object sender, EventArgs e)
      //单击数字命令按钮的事件处理程序
      {
       }  private void buttonop_Click(object sender, EventArgs e)问下,这2个事件是怎么调用的呢,
    比如 buttond_Click () 
    要在每个数字button1_click()中写上buttond_Click();吗好像默认的事件 是button1_click()    button2_click()
      

  4.   

    我写了个弱智的。就是不知道下面那个地方怎么简化一下using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace 计算器
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private double x;// 运算前数值
            private double y;// 运算后数值
            private string js;//运算符        private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.Text = "";
            }
            private void but_click(string text)
            {//操作数值按钮,文本框的显示            if (js == "")
                {
                    textBox1.Text = textBox1.Text + text;
                }
                else
                {
                    textBox1.Text = text;
                }        }
          
            //以下是+-*/
            private void button13_Click(object sender, EventArgs e)
            {
                x = Convert.ToDouble(textBox1.Text);
                js = button13.Text;
            }        private void button12_Click(object sender, EventArgs e)
            {
                x = Convert.ToDouble(textBox1.Text);
                js = button12.Text;
            }        private void button11_Click(object sender, EventArgs e)
            {
                x = Convert.ToDouble(textBox1.Text);
                js = button11.Text;
            }        private void button14_Click(object sender, EventArgs e)
            {
                x = Convert.ToDouble(textBox1.Text);
                js = button14.Text;
            }        //清除按钮
            private void button16_Click(object sender, EventArgs e)
            {
                textBox1.Text = "";
                x = 0;
                y = 0;
                js = "";
            }        //等号按钮
            private void button15_Click(object sender, EventArgs e)
            {
                y = Convert.ToDouble(textBox1.Text);            if (js == "/" && y == 0)
                {
                    MessageBox.Show("除数为0错误!");
                    return;
                }
                else
                {
                    switch (js)
                    {
                        case "+":
                            textBox1.Text = (x + y).ToString();
                            break;
                        case "-":
                            textBox1.Text = (x - y).ToString();
                            break;
                        case "*":
                            textBox1.Text = (x * y).ToString();
                            break;
                    }
                }
            }        //以下数值按钮调用but_click()事件 ,主要是怎么把下面的简化,只写一个???
            private void button9_Click(object sender, EventArgs e)
            {
                but_click(this.button9.Text);
            }        private void button6_Click(object sender, EventArgs e)
            {
                but_click(this.button6.Text);
            }        private void button8_Click(object sender, EventArgs e)
            {
                but_click(this.button8.Text);
            }        private void button7_Click(object sender, EventArgs e)
            {
                but_click(this.button7.Text);
            }        private void button5_Click(object sender, EventArgs e)
            {
                but_click(this.button5.Text);
            }        private void button4_Click(object sender, EventArgs e)
            {
                but_click(this.button4.Text);
            }        private void button3_Click(object sender, EventArgs e)
            {
                but_click(this.button3.Text);
            }        private void button2_Click(object sender, EventArgs e)
            {
                but_click(this.button2.Text);
            }        private void button1_Click(object sender, EventArgs e)
            {
                but_click(this.button1.Text);
            }
            private void button10_Click(object sender, EventArgs e)
            {
                but_click(this.button10.Text);
            }    }
    }
      

  5.   

    右边窗口不是有click事件吗? 你写上不就行了!!!!!
      

  6.   

    http://topic.csdn.net/u/20090928/21/a476920f-49aa-4975-a1cd-2d6fc806698f.html
      

  7.   

    12楼的有问题,首先掉了计算除,而且 private void button3_Click(object sender, EventArgs e)
            {
                but_click(this.button3.Text);
            }你这样写就只能计算一位数的计算