首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

初学者求指点

2012-05-27 
菜鸟求指点刚学习面向对象,写了个计算器的代码。求大虾们指点下有什么地方要改进。C# codeusing Systemusin

菜鸟求指点
刚学习面向对象,写了个计算器的代码。求大虾们指点下有什么地方要改进。

C# code
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 jisuanqi2{    public partial class Form1 : Form    {        string first,second,sign;//first存放第一个数据,second存放第二个数据,sign判断哪个算数。        bool norepeat=false,sign2=false;//sign解决重复按键。sign2解决用户先按下符号。        double final;//存放最后结果。        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)//数字1-9  button1-9        {            textBox1.Text = textBox1.Text + button1.Text;            second = textBox1.Text;            sign2 = true;        }        private void button2_Click(object sender, EventArgs e)        {            textBox1.Text = textBox1.Text + button2.Text;            second = textBox1.Text;            sign2 = true;        }        private void button3_Click(object sender, EventArgs e)        {            textBox1.Text = textBox1.Text + button3.Text;            second = textBox1.Text;            sign2 =true;        }        private void button4_Click(object sender, EventArgs e)        {            textBox1.Text = textBox1.Text + button4.Text;            second = textBox1.Text;            sign2 = true;        }        private void button5_Click(object sender, EventArgs e)        {            textBox1.Text = textBox1.Text + button5.Text;            second = textBox1.Text;            sign2 = true;        }        private void button6_Click(object sender, EventArgs e)        {            textBox1.Text = textBox1.Text + button6.Text;            second = textBox1.Text;            sign2 = true;        }        private void button7_Click(object sender, EventArgs e)        {            textBox1.Text = textBox1.Text + button7.Text;            second = textBox1.Text;            sign2 = true;        }        private void button8_Click(object sender, EventArgs e)        {            textBox1.Text = textBox1.Text + button8.Text;            second = textBox1.Text;            sign2 = true;        }        private void button9_Click(object sender, EventArgs e)        {            textBox1.Text = textBox1.Text + button9.Text;            second = textBox1.Text;            sign2 = true;        }        private void button13_Click(object sender, EventArgs e)//小数点        {            textBox1.Text = textBox1.Text + button0.Text;            second = textBox1.Text;            sign2 = true;        }        private void button14_Click(object sender, EventArgs e)        {            textBox1.Text = textBox1.Text + button14.Text;            second = textBox1.Text;            sign2 = true;        }        private void plus_Click(object sender, EventArgs e)        {               if(sign2==true)            {            if (norepeat == false)            {                sign = "+";                first = textBox1.Text;                textBox1.Text = "";                norepeat = true;            }            else            {                return;            }            }        }        private void minus_Click(object sender, EventArgs e)        {            if (sign2 == true)            {                if (norepeat == false)                {                    sign = "-";                    first = textBox1.Text;                    textBox1.Text = "";                    norepeat = true;                }                else                {                    return;                }            }        }        private void multiple_Click(object sender, EventArgs e)        {            if (sign2 == true)            {                if (norepeat == false)                {                    sign = "*";                    first = textBox1.Text;                    textBox1.Text = "";                    norepeat = true;                }                else                {                    return;                }            }        }        private void weed_Click(object sender, EventArgs e)        {            if (sign2 == true)            {                if (norepeat == false)                {                    sign = "/";                    first = textBox1.Text;                    textBox1.Text = "";                    norepeat = true;                }                else                {                    return;                }            }        }        private void button20_Click(object sender, EventArgs e)        {            switch (sign)            {                case "+": final = double.Parse(first) + double.Parse(second); break;                case "-": final = double.Parse(first) - double.Parse(second); break;                case "*": final = double.Parse(first) * double.Parse(second); break;                case "/": final = double.Parse(first) / double.Parse(second); break;            }             textBox1.Text = Convert.ToString(final);            first = Convert.ToString(final);            norepeat = false;        }        private void emptly_Click(object sender, EventArgs e)        {            first = "";            second = "";            textBox1.Text = "";            norepeat = false;            sign2 = false;        }        private void Form1_Load(object sender, EventArgs e)        {            this.MaximizeBox = false;            this.MinimizeBox = false;        }          }} 




[解决办法]

 private void button1_Click(object sender, EventArgs e)//数字1-9 button1-9

它们用一个click事件

sender as Button

然后swich....case判断Button的ID...做不同的事
[解决办法]

给这几个按钮绑定一个click事件就可以。。。

string id = (sender as Button).ID;

switch (id)
{
case "Button1":
这里写你现在button1_Click事件里的代码
break;
case "Button2":
这里写你现在button2_Click事件里的代码
break;
....
default:
break;
}
[解决办法]
探讨
给这几个按钮绑定一个click事件就可以。。。

string id = (sender as Button).ID;

switch (id)
{
case "Button1":
这里写你现在button1_Click事件里的代码
break;
case "Button2":
这里写你现在button2_Click事件里的代码
break;
.……

[解决办法]
textBox1.Text = textBox1.Text + (sender as Button).Text;second = textBox1.Text;
sign2 = true;

[解决办法]
探讨

引用:
给这几个按钮绑定一个click事件就可以。。。

string id = (sender as Button).ID;

switch (id)
{
case "Button1":
这里写你现在button1_Click事件里的代码
break;
case "Button2":
这里写你现在button2_Click事件里的代码
……

[解决办法]
探讨

引用:

引用:
给这几个按钮绑定一个click事件就可以。。。

string id = (sender as Button).ID;

switch (id)
{
case "Button1":
这里写你现在button1_Click事件里的代码
break;
case "Button2":
这里写你现……

热点排行