2012-02-12 81 views
2

我正在制作一个使用Windows Forms Application的计算器,目前我很难过。 这是我的应用程序到目前为止,它只能添加,但任何数量的数量。C#中的计算器操作难倒

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 Calculator 
{ 
public partial class Form1 : Form 
{ 
    int num; 
    int answer = 0; 
    int i; 


    public Form1() 
    { 
     InitializeComponent(); 
    } 



    private void plusButton_Click(object sender, EventArgs e) 
    {   
     answer += System.Convert.ToInt32(textBox1.Text);    
     ClearTextbox(); 
    } 

    private void minusButton_Click(object sender, EventArgs e) 
    { 
     //answer -= System.Convert.ToInt32(textBox1.Text); 
     //ClearTextbox(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void divideButton_Click(object sender, EventArgs e) 
    { 

    } 

    private void multiplyButton_Click(object sender, EventArgs e) 
    { 

    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     num = 1; 
     displayInt(num); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     num = 2; 
     displayInt(num); 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     num = 3; 
     displayInt(num); 
    } 

    private void button4_Click(object sender, EventArgs e) 
    { 
     num = 4; 
     displayInt(num); 
    } 

    private void button5_Click(object sender, EventArgs e) 
    { 
     num = 5; 
     displayInt(num); 
    } 

    private void button6_Click(object sender, EventArgs e) 
    { 
     num = 6; 
     displayInt(num); 
    } 

    private void button7_Click(object sender, EventArgs e) 
    { 
     num = 7; 
     displayInt(num); 
    } 

    private void button8_Click(object sender, EventArgs e) 
    { 
     num = 8; 
     displayInt(num); 
    } 

    private void button9_Click(object sender, EventArgs e) 
    { 
     num = 9; 
     displayInt(num); 
    } 

    private void button0_Click(object sender, EventArgs e) 
    { 
     num = 0; 
     displayInt(num); 
    } 

    private void resetButton_Click(object sender, EventArgs e) 
    { 
     ClearTextbox(); 
     num = 0; 
     answer = 0; 

    } 

    public void displayInt(int num) 
    { 
     textBox1.Text = textBox1.Text + num.ToString(); 
    } 

    public void ClearTextbox() 
    { 
     textBox1.Clear(); 
    } 

    public void DisplayAnswer(int answer) 
    { 
     answer += System.Convert.ToInt32(textBox1.Text); //Answer = Answer + Textbox Stuff 


     textBox1.Clear(); 
     textBox1.Text = answer.ToString(); 
    } 

    private void equalsButton_Click(object sender, EventArgs e) 
    { 
     DisplayAnswer(answer); 
     num = 0; 
     answer = 0; 
    } 


} 
} 

我不知道是否有办法等到另一个数字键被按下,然后做x + y。我听说过事件处理程序,但对我来说这是一个相当模糊的话题。

这里有一个画面:http://img196.imageshack.us/img196/4127/12464e13e5154a949a9a457.png

*我希望它能够做到操作更然后2号。

谢谢!

+1

这是功课吗? – 2012-02-12 00:47:35

+0

您所有的button_Click例程都是事件处理程序! – 2012-02-12 00:47:48

+0

@ M.Babcock不要,这样做是为了好玩。 – Hexo 2012-02-12 00:49:18

回答

2

你的代码有点混乱,很难弄清楚你的意图。这段代码的帮助将很快到来。我建议以下this simple calculator tutorial。它非常好地解释了概念和步骤,包括按钮事件。

+0

谢谢你,看起来不错。将检查出来。 – Hexo 2012-02-12 00:57:15

0

您需要有一个“+” - 按钮和一个“=” - 按钮。然后你的计算器有两个数字域两种状态:进入第一个数字

2)进入第二个数字

1)你可以有正在被输入的号码变量说法。

int field = 1; 

当按下数字按钮时,该数字必须附加到相应的字段。

当按下“+” - 按钮状态改变为

field = 2; 

现在数字将被附加到第二个字段。

当您按下“=” - 按钮时,将现在仍然是字符串的两个数字转换为int's,将它们添加并输出到结果字段。将状态恢复到

field = 1; 

也有“明确” - 按钮将清除字段,并设置field = 1;


注:正如保罗萨西克说,你的代码是有点乱。我会帮助,如果你给你的按钮有意义的名字,如btnDigit1,btnDigit2,btnPlus, btnEquals, btnClear。当你在设计器中双击一个按钮时,将会创建一个类似于btnPlus_Click的事件处理程序。这比button17_Click更清晰。