Reputation: 215
I was making a calculator using Windows Forms Application and I'm currently stumped. Here's my application so far, it can only do addition, but any amount of numbers.
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;
}
}
}
I'm not sure if there's a way to wait until another number key is pressed, and then do x + y. I heard about event handlers, but it's quite a vague topic to me.
Here's a picture: http://img196.imageshack.us/img196/4127/12464e13e5154a949a9a457.png
*I want it to be able to do operations to more then 2 numbers.
Thanks!
Upvotes: 2
Views: 2378
Reputation: 112537
You would need to have a "+"-button and a "="-button. Then your calculator has two number fields and two states:
1) Entering first number
2) Entering second number
You could have a variable saying which number is being entered.
int field = 1;
When pressing a digit button, this digit has to be appended to the corresponding field.
When pressing the "+"-button change the state to
field = 2;
Now the digits will be appended to the second field.
When you press the "="-button, convert the two numbers, which are still strings now, to int
's, add them and output them to a result field. Set the state back to
field = 1;
Also a "clear"-button would clear the fields and set field = 1;
Note: As Paul Sasik says, your code is a bit messy. I would help, if you gave your buttons meaningful names like btnDigit1
, btnDigit2
, btnPlus
, btnEquals
, btnClear
. When you double-click a button in the designer an event handler like btnPlus_Click
will be created. This is clearer than button17_Click
.
Upvotes: 0
Reputation: 81509
Your code is a bit messy and it's hard to figure out your intent. Help with this code will be long in coming. I suggest following this simple calculator tutorial. It explains the concepts and steps very nicely, including button events.
Upvotes: 2