2012-02-12 82 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更清晰。