Wednesday, March 16, 2011

Simple calculator using Windows form Application with C# in VS2008



File->new->project
the sceenshot given below will be shown and select windows form Appliction
snapshot of form that i have built is shown below


Double click on form1(Design view) to open form1.cs(Source view)

form1.cs

 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  
   {  
     public Form1()  
     {  
       InitializeComponent();  
     }  
     float val, val1,val2,val3,val4,answer;  
     int ch,a,b,c,d;  
     bool flag;  
    //Textbox1 is 1st textbox shown in above form for entering value  
    //Textbox2 is 2nd textbox shown in above form ,only used for display result   
     private void button1_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "1";  
     }  
     private void button2_Click(object sender, EventArgs e)  
     {  
       textBox1.Text +="2";  
     }  
     private void button3_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "3";  
     }  
     private void button4_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "4";  
     }  
      private void button5_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "5";  
     }  
     private void button6_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "6";  
     }  
     private void button7_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "7";  
     }  
     private void button8_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "8";  
     }  
     private void button9_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "9";  
     }  
     private void button10_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += "0";  
     }  
     private void button12_Click(object sender, EventArgs e)    //ADDITION  
     {  
       ch = 1;  
       if (a == 0)  
       {  
         flag = float.TryParse(textBox1.Text, out val);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         textBox1.Clear();  
         textBox1.Focus();  
         a = 1;  
         b = 2;  
         c = 2;  
         d = 2;  
       }  
       else if (a == 1)  
       {  
         flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val + val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val.ToString() + "+" + val1.ToString() + "=" + answer.ToString();  
         a = 0;  
         b = 0;  
         c = 0;  
         d = 0;  
       }  
       else  
       {  
         MessageBox.Show("click right button", "Syntax Error");  
       }  
     }  
     private void button13_Click(object sender, EventArgs e)  
     {  
       Close();  
     }  
     private void button14_Click(object sender, EventArgs e)//multiply  
     {  
       ch = 2;  
       if(b==0)  
       {  
       flag = float.TryParse(textBox1.Text, out val2);  
       if (flag == false)  
       {  
         MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
         textBox1.Focus();  
       }  
       textBox1.Clear();  
       textBox1.Focus();  
         b=1;  
         a=2;  
         c=2;  
         d=2;  
       }  
       else if(b==1)  
       {flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val2 * val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val2.ToString() + "*" + val1.ToString() + "=" + answer.ToString();  
         b=0;  
         a=0;  
         c=0;  
         d=0;  
       }  
       else  
       {  
         MessageBox.Show("click right button", "Syntax Error");  
       }  
     }  
     private void button15_Click(object sender, EventArgs e)  
     {  
       textBox1.Clear();  
       textBox2.Clear();  
       textBox1.Focus();  
       a = 0;  
       b = 0;  
       c = 0;  
       d = 0;  
     }  
     private void button17_Click(object sender, EventArgs e)//MINUS  
     {  
       ch = 3;  
       if (c == 0)  
       {  
         flag = float.TryParse(textBox1.Text, out val3);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Error Message");  
           textBox1.Focus();  
         }  
         textBox1.Clear();  
         textBox1.Focus();  
         c= 1;  
         a = 2;  
         b = 2;  
         d = 2;  
       }  
       else if(c==1)  
       {  
         flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val3 - val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val3.ToString() + "-" + val1.ToString() + "=" + answer.ToString();  
         c = 0;  
         a = 0;  
         b = 0;  
         d = 0;  
       }  
       else  
       {  
         MessageBox.Show("click right button", "Syntax Error");  
       }  
     }  
     private void button16_Click(object sender, EventArgs e)    //divide  
     {  
       ch = 4;  
       if (d == 0)  
       {  
        flag = float.TryParse(textBox1.Text, out val4);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Error Message");  
           textBox1.Focus();  
         }  
         textBox1.Clear();  
         textBox1.Focus();  
         d = 1;  
         a = 2;  
         b = 2;  
         c = 2;  
       }  
       else if(d==1)  
       {  
         flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val4 / val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val4.ToString() + "/" + val1.ToString() + "=" + answer.ToString();  
         a = 0;  
         b = 0;  
         c = 0;  
         d = 0;  
       }  
       else  
       {  
         MessageBox.Show("click right button", "Syntax Error");  
       }  
     }  
       private void button11_Click(object sender, EventArgs e)  
     {  
       switch(ch)  
       {  
         case 1:  flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val + val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val.ToString() + "+" + val1.ToString() + "=" + answer.ToString();  
         a = 0;  
         b = 0;  
         c = 0;  
         d = 0;  
         break;  
         case 2:  
          flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val2 * val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val2.ToString() + "*" + val1.ToString() + "=" + answer.ToString();  
         a = 0;  
         b = 0;  
         c = 0;  
         d = 0;  
         break;  
         case 3:  
         flag = float.TryParse(textBox1.Text, out val1);  
         if (flag == false)  
         {  
           MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
           textBox1.Focus();  
         }  
         answer = val3- val1;  
         textBox1.Text = answer.ToString();  
         textBox2.Text = val3.ToString() + "-" + val1.ToString() + "=" + answer.ToString();  
         a = 0;  
         b = 0;  
         c = 0;  
         d = 0;  
         break;  
         case 4:  
         flag = float.TryParse(textBox1.Text, out val1);  
           if (flag == false)  
           {  
             MessageBox.Show("Invaid Value Entered", "Syntax Error Message");  
             textBox1.Focus();  
           }  
           answer = val4 / val1;  
           textBox1.Text = answer.ToString();  
           textBox2.Text = val4.ToString() + "/" + val1.ToString() + "=" + answer.ToString();  
           a = 0;  
           b = 0;  
           c = 0;  
           d = 0;  
           break;  
       }            
     }  
     private void button18_Click(object sender, EventArgs e)  
     {  
       textBox1.Text += ".";  
     }  
     private void button19_Click(object sender, EventArgs e)  
     {  
       textBox1.Clear();  
       textBox2.Clear();  
       textBox1.Focus();  
     }  
   }  
 }  


Look At the property window of ADD button


In this I have given Text=&ADD that will make A underlined in ADD, this is for we can use ADD button using keyboard by using command ALT+A(underlined Alphabet)

Properties window of textbox2

Set Multiline and Read only Property TRUE in Textbox2 properties window

1 comment:

Unknown said...

Very Simple coding to learn........Keep it up All the best !!!!

Popular Posts