Following are the steps to make calculator using windows form-
1. Create a new wpf project in visual studio.
File>New>Project>windows>wpf application
2. Add a new form in the solution
Right click solution then click add>new item and a new windows form.
3.Add textbox and buttons from the tool box in the form and change colors if you want.
4.Right click button then properties>name (change name). Do this for all of the buttons and give them appropriate names.
5. Double click the buttons to add buttonClick methods in Form1.cs file.
6.Implement buttonClick methods. On click of a digit add the respective number in the textbox.
Example-
7.Now we will implement add,sub,mul,div,mod etc methods on the respective button clicks.
a. Declare boolean variables to know which operator is currently in use and two doubles variables.
- double operand1, operand2;
- bool add, sub, mul, div,mod;
b. Implement operations-
Here, if the text box is empty , means no operation can be performed. If boolean variable add is true that means addition needs to be performed and call compute() ,otherwise assign operand1 with text box value and make add variable true. Same goes for other operators.
|
- //Subtraction Method
- private void sub_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- return;
- else if (add == true || sub == true || mul == true || div == true ||mod==true)
- {
- compute();
- }
- else
- {
- operand1 = double.Parse(textBox1.Text);
- }
- sub = true;
- textBox1.Clear();
- }
- //Multiplication Method
- private void mul_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- return;
- else if (add == true || sub == true || mul == true || div == true || mod == true)
- {
- compute();
- }
- else
- {
- operand1 = double.Parse(textBox1.Text);
- }
- mul = true;
- textBox1.Clear();
- }
- //Division Method
- private void div_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- return;
- else if (add == true || sub == true || mul == true || div == true || mod == true)
- {
- compute();
- }
- else
- {
- operand1 = double.Parse(textBox1.Text);
- }
- div = true;
- textBox1.Clear();
- }
- //Modulus Method
- private void mod_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- return;
- else if (add == true || sub == true || mul == true || div == true || mod == true)
- {
- compute();
- }
- else
- {
- operand1 = double.Parse(textBox1.Text);
- }
- mod = true;
- textBox1.Clear();
- }
- private void equal_button_Click(object sender, EventArgs e)
- {
- compute();
- textBox1.Text = operand1.ToString();
- }
- private void period_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- {
- textBox1.Text += "0.";
- }
- else
- {
- textBox1.Text += ".";
- }
- }
This method will clear the textbox.
- private void clear_button_Click(object sender, EventArgs e)
- {
- textBox1.Clear();
- }
- private void back_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- return;
- int len = (textBox1.Text).Length;
- string a=textBox1.Text.Substring(0,len-1);
- textBox1.Text = a;
- }
- private void buttonAllClear_Click(object sender, EventArgs e)
- {
- textBox1.Text = "";
- textBox1.Clear();
- }
- //This method computes the result of operation between two numbers.
- private void compute()
- {
- operand2 = double.Parse(textBox1.Text);
- if (add)
- {
- operand1 = operand1 + operand2;
- add = false;
- return;
- }
- else if (sub)
- {
- operand1 = operand1 - operand2;
- sub = false;
- return;
- }
- else if (mul)
- {
- operand1 = operand1 * operand2;
- mul = false;
- return;
- }
- else if (div)
- {
- operand1 = operand1 / operand2;
- div = false;
- return;
- }
- else if (mod)
- {
- operand1 = operand1 % operand2;
- mod = false;
- return;
- }
- else
- return;
- }
This method changes the value of text box everytime a value is inserted.
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- textBox1.Text = textBox1.Text;
- }
This is the complete code-
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Calculator
- {
- public partial class Form1 : Form
- {
- double operand1, operand2;
- bool add, sub, mul, div,mod;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- textBox1.Text = textBox1.Text + "1";
- }
- private void button2_Click(object sender, EventArgs e)
- {
- textBox1.Text = textBox1.Text + "2";
- }
- private void button3_Click(object sender, EventArgs e)
- {
- textBox1.Text = textBox1.Text + "3";
- }
- private void button4_Click(object sender, EventArgs e)
- {
- textBox1.Text = textBox1.Text + "4";
- }
- private void button5_Click(object sender, EventArgs e)
- {
- textBox1.Text = textBox1.Text + "5";
- }
- private void button6_Click(object sender, EventArgs e)
- {
- textBox1.Text = textBox1.Text + "6";
- }
- private void button7_Click(object sender, EventArgs e)
- {
- textBox1.Text = textBox1.Text + "7";
- }
- private void button8_Click(object sender, EventArgs e)
- {
- textBox1.Text = textBox1.Text + "8";
- }
- private void button9_Click(object sender, EventArgs e)
- {
- textBox1.Text = textBox1.Text + "9";
- }
- private void add_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- return;
- else if (add==true || sub==true || mul==true || div==true || mod == true)
- {
- compute();
- }
- else
- {
- operand1 = double.Parse(textBox1.Text);
- }
- add = true;
- textBox1.Clear();
- }
- private void sub_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- return;
- else if (add == true || sub == true || mul == true || div == true ||mod==true)
- {
- compute();
- }
- else
- {
- operand1 = double.Parse(textBox1.Text);
- }
- sub = true;
- textBox1.Clear();
- }
- private void mul_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- return;
- else if (add == true || sub == true || mul == true || div == true || mod == true)
- {
- compute();
- }
- else
- {
- operand1 = double.Parse(textBox1.Text);
- }
- mul = true;
- textBox1.Clear();
- }
- private void div_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- return;
- else if (add == true || sub == true || mul == true || div == true || mod == true)
- {
- compute();
- }
- else
- {
- operand1 = double.Parse(textBox1.Text);
- }
- div = true;
- textBox1.Clear();
- }
- private void mod_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- return;
- else if (add == true || sub == true || mul == true || div == true || mod == true)
- {
- compute();
- }
- else
- {
- operand1 = double.Parse(textBox1.Text);
- }
- mod = true;
- textBox1.Clear();
- }
- private void equal_button_Click(object sender, EventArgs e)
- {
- compute();
- textBox1.Text = operand1.ToString();
- }
- private void period_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- {
- textBox1.Text += "0.";
- }
- else
- {
- textBox1.Text += ".";
- }
- }
- private void clear_button_Click(object sender, EventArgs e)
- {
- textBox1.Clear();
- }
- private void back_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text == "")
- return;
- int len = (textBox1.Text).Length;
- string a=textBox1.Text.Substring(0,len-1);
- textBox1.Text = a;
- }
- private void compute()
- {
- operand2 = double.Parse(textBox1.Text);
- if (add)
- {
- operand1 = operand1 + operand2;
- add = false;
- return;
- }
- else if (sub)
- {
- operand1 = operand1 - operand2;
- sub = false;
- return;
- }
- else if (mul)
- {
- operand1 = operand1 * operand2;
- mul = false;
- return;
- }
- else if (div)
- {
- operand1 = operand1 / operand2;
- div = false;
- return;
- }
- else if (mod)
- {
- operand1 = operand1 % operand2;
- mod = false;
- return;
- }
- else
- return;
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- textBox1.Text = textBox1.Text;
- }
- private void buttonAllClear_Click(object sender, EventArgs e)
- {
- textBox1.Text = "";
- textBox1.Clear();
- }
- private void button00_Click(object sender, EventArgs e)
- {
- textBox1.Text += "00";
- }
- private void button0_Click(object sender, EventArgs e)
- {
- textBox1.Text += "0";
- }
- }
- }
8. Press cntr+f5 to run the program.






Comments
Post a Comment