How to make a Calculator in C#

In this tutorial, I will show you how to create a calculator using C#.net. This is a simple calculator with functions that works accurately. It also contains procedures that are very easy to follow and can be done in a short period of time.

Let's begin:

Step 1. Open Microsoft Visual Studio 2008 and create new Windows Form Application for C#. After that, do the form just like this. cal fig 1 Step 2. Go to the Solution Explorer, double-click the “View Code” to fire the code editor. cal fig2 Step 3. In the code editor, declare the variables that are needed.
  1.  double total = 0;
  2.  string LOperator;
Step 4. Set the following codes for the function of the (0-9 ,clear and .) buttons .
  1.   private void btn1_Click(object sender, EventArgs e)
  2.         {
  3.             if (txtView.Text == "0")
  4.             {
  5.                 txtView.Clear();
  6.                 txtView.Text += btn1.Text;
  7.             }
  8.             else
  9.             {
  10.                 txtView.Text += btn1.Text;
  11.             }
  12.         }
  13.  
  14.         private void btn2_Click(object sender, EventArgs e)
  15.         {
  16.             if (txtView.Text == "0")
  17.             {
  18.                 txtView.Clear();
  19.                 txtView.Text += btn2.Text;
  20.             }
  21.             else
  22.             {
  23.                 txtView.Text += btn2.Text;
  24.             }
  25.         }
  26.  
  27.         private void btn3_Click(object sender, EventArgs e)
  28.         {
  29.             if (txtView.Text == "0")
  30.             {
  31.                 txtView.Clear();
  32.                 txtView.Text += btn3.Text;
  33.             }
  34.             else
  35.             {
  36.                 txtView.Text += btn3.Text;
  37.             }
  38.         }
  39.  
  40.         private void btn0_Click(object sender, EventArgs e)
  41.         {
  42.             if (txtView.Text == "0")
  43.             {
  44.                 txtView.Clear();
  45.                 txtView.Text += btn0.Text;
  46.             }
  47.             else
  48.             {
  49.                 txtView.Text += btn0.Text;
  50.             }
  51.         }
  52.  
  53.         private void btn8_Click(object sender, EventArgs e)
  54.         {
  55.             if (txtView.Text == "0")
  56.             {
  57.                 txtView.Clear();
  58.                 txtView.Text += btn8.Text;
  59.             }
  60.             else
  61.             {
  62.                 txtView.Text += btn8.Text;
  63.             }
  64.         }
  65.  
  66.         private void btn7_Click(object sender, EventArgs e)
  67.         {
  68.             if (txtView.Text == "0")
  69.             {
  70.                 txtView.Clear();
  71.                 txtView.Text += btn7.Text;
  72.             }
  73.             else
  74.             {
  75.                 txtView.Text += btn7.Text;
  76.             }
  77.            
  78.         }
  79.  
  80.         private void btn9_Click(object sender, EventArgs e)
  81.         {
  82.             if (txtView.Text == "0")
  83.             {
  84.                 txtView.Clear();
  85.                 txtView.Text += btn9.Text;
  86.             }
  87.             else
  88.             {
  89.                 txtView.Text += btn9.Text;
  90.             }
  91.            
  92.         }
  93.  
  94.         private void btn6_Click(object sender, EventArgs e)
  95.         {
  96.             if (txtView.Text == "0")
  97.             {
  98.                 txtView.Clear();
  99.                 txtView.Text += btn6.Text;
  100.             }
  101.             else
  102.             {
  103.                 txtView.Text += btn6.Text;
  104.             }
  105.              
  106.         }
  107.  
  108.         private void btn5_Click(object sender, EventArgs e)
  109.         {
  110.             if (txtView.Text == "0")
  111.             {
  112.                 txtView.Clear();
  113.                 txtView.Text += btn5.Text;
  114.             }
  115.             else
  116.             {
  117.                 txtView.Text += btn5.Text;
  118.             }
  119.         }
  120.  
  121.         private void btn4_Click(object sender, EventArgs e)
  122.         {
  123.             if (txtView.Text == "0")
  124.             {
  125.                 txtView.Clear();
  126.                 txtView.Text += btn4.Text;
  127.             }
  128.             else
  129.             {
  130.                 txtView.Text += btn4.Text;
  131.             }
  132.         }
  133.  
  134.         private void btnDote_Click(object sender, EventArgs e)
  135.         {
  136.             if (!txtView.Text.Contains("."))
  137.             {
  138.                 txtView.Text += btnDote.Text;
  139.             }
  140.            
  141.         }
  142.  
  143.         private void btnClear_Click(object sender, EventArgs e)
  144.         {
  145.             txtView.Text = "0";
  146.             total = 0;
  147.            
  148.         }
Step 5. Set the following codes for the function of the (x,/,+,-) MDAS calculation.
  1.   private void btnPlus_Click(object sender, EventArgs e)
  2.         {
  3.             total += double.Parse(txtView.Text);
  4.             LOperator = "plus";
  5.             txtView.Clear();
  6.  
  7.  
  8.         }
  9.        private void btnminus_Click(object sender, EventArgs e)
  10.         {
  11.             total += double.Parse(txtView.Text);
  12.             LOperator = "minus";
  13.             txtView.Clear();
  14.  
  15.         }
  16.        private void btnMultiply_Click(object sender, EventArgs e)
  17.        {
  18.            total += double.Parse(txtView.Text);
  19.            LOperator = "multiply";
  20.            txtView.Clear();
  21.        }
  22.  
  23.        private void btnDivide_Click(object sender, EventArgs e)
  24.        {
  25.            total += double.Parse(txtView.Text);
  26.            LOperator = "divide";
  27.            txtView.Clear();
  28.        }
  29.         private void btnEqual_Click(object sender, EventArgs e)
  30.         {
  31.  
  32.             switch (LOperator){
  33.                 case "plus":
  34.                     total = total + double.Parse(txtView.Text);
  35.                     txtView.Text = total.ToString();
  36.                     total = 0;
  37.                     LOperator = "";
  38.                     break ;
  39.                 case "minus":
  40.                     total = total - double.Parse(txtView.Text);
  41.                     txtView.Text = total.ToString();
  42.                     total = 0;
  43.                     LOperator = "";
  44.                     break;
  45.  
  46.                 case "multiply":
  47.                     total = total * double.Parse(txtView.Text);
  48.                     txtView.Text = total.ToString();
  49.                     total = 0;
  50.                     LOperator = "";
  51.                     break;
  52.                 case "divide":
  53.                     total = total / double.Parse(txtView.Text);
  54.                     txtView.Text = total.ToString();
  55.                     total = 0;
  56.                     LOperator = "";
  57.                     break;
  58.                 default :
  59.                     txtView.Text = total.ToString();
  60.                     break;
  61.  
  62.             }
  63.                
  64.              
  65.              
  66.              
  67.         }
Download the complete source code and run it on your computer. For all students who need a programmer for your thesis system or anyone who needs a source code in any programming languages. You can contact me @ : Email – [email protected] Mobile No. – 09305235027 – tnt

Add new comment