Calculator In with a Flat Design C#

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Runtime.InteropServices;  
  8. using System.Text;  
  9. using System.Threading.Tasks;  
  10. using System.Windows.Forms;  
  11. namespace Calculator  
  12. {  
  13.     public partial class Form1: Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         //vars  
  20.         string oper;  
  21.         decimal num1, num2, result = 0;  
  22.         private void Form1_Load(object sender, EventArgs e)  
  23.         {  
  24.             // txtDisplay.Text = "0";  
  25.         }  
  26.         //1  
  27.         private void btn1_Click(object sender, EventArgs e)  
  28.         {  
  29.             txtDisplay.Text += btn1.Text;  
  30.         }  
  31.         //2  
  32.         private void btn2_Click(object sender, EventArgs e)  
  33.         {  
  34.             txtDisplay.Text += btn2.Text;  
  35.         }  
  36.         //3  
  37.         private void btn3_Click(object sender, EventArgs e)  
  38.         {  
  39.             txtDisplay.Text += btn3.Text;  
  40.         }  
  41.         //4  
  42.         private void btn4_Click(object sender, EventArgs e)  
  43.         {  
  44.             txtDisplay.Text += btn4.Text;  
  45.         }  
  46.         //5  
  47.         private void btn5_Click(object sender, EventArgs e)  
  48.         {  
  49.             txtDisplay.Text += btn5.Text;  
  50.         }  
  51.         //6  
  52.         private void btn6_Click(object sender, EventArgs e)  
  53.         {  
  54.             txtDisplay.Text += btn6.Text;  
  55.         }  
  56.         //7  
  57.         private void btn7_Click(object sender, EventArgs e)  
  58.         {  
  59.             txtDisplay.Text += btn7.Text;  
  60.         }  
  61.         //8  
  62.         private void btn8_Click(object sender, EventArgs e)  
  63.         {  
  64.             txtDisplay.Text += btn8.Text;  
  65.         }  
  66.         //9  
  67.         private void btn9_Click(object sender, EventArgs e)  
  68.         {  
  69.             txtDisplay.Text += btn9.Text;  
  70.         }  
  71.         //0  
  72.         private void btn0_Click(object sender, EventArgs e)  
  73.         {  
  74.             txtDisplay.Text += btn0.Text;  
  75.         }  
  76.         //,  
  77.         private void btnComma_Click(object sender, EventArgs e)  
  78.         {  
  79.             txtDisplay.Text += btnComma.Text;  
  80.         }  
  81.         // +|-  
  82.         private void btnPlusOrMinus_Click(object sender, EventArgs e)  
  83.         {  
  84.             if (txtDisplay.Text.Contains("-"))  
  85.             {  
  86.                 txtDisplay.Text = txtDisplay.Text.Remove(0, 1);  
  87.             }  
  88.             else  
  89.             {  
  90.                 txtDisplay.Text = "-" + txtDisplay.Text;  
  91.             }  
  92.         }  
  93.         //Plus  
  94.         private void btnPlus_Click(object sender, EventArgs e)  
  95.         {  
  96.             num1 = decimal.Parse(txtDisplay.Text);  
  97.             oper = "+";  
  98.             txtDisplay.Text = string.Empty;  
  99.             textBox1.Text = num1.ToString() + " + ";  
  100.         }  
  101.         //SUB  
  102.         private void btnSub_Click(object sender, EventArgs e)  
  103.         {  
  104.             num1 = decimal.Parse(txtDisplay.Text);  
  105.             oper = "-";  
  106.             txtDisplay.Text = string.Empty;  
  107.             textBox1.Text = num1.ToString() + " - ";  
  108.         }  
  109.         //multiply  
  110.         private void btnTimes_Click(object sender, EventArgs e)  
  111.         {  
  112.             num1 = decimal.Parse(txtDisplay.Text);  
  113.             oper = "x";  
  114.             txtDisplay.Text = string.Empty;  
  115.             textBox1.Text = num1.ToString() + " x ";  
  116.         }  
  117.         //divide  
  118.         private void btnDivide_Click(object sender, EventArgs e)  
  119.         {  
  120.             num1 = decimal.Parse(txtDisplay.Text);  
  121.             oper = "/";  
  122.             txtDisplay.Text = string.Empty;  
  123.             textBox1.Text = num1.ToString() + " / ";  
  124.         }  
  125.         private void btnEqual_Click(object sender, EventArgs e)  
  126.         {  
  127.             num2 = decimal.Parse(txtDisplay.Text);  
  128.             textBox1.Text = string.Empty;  
  129.             switch (oper)  
  130.             {  
  131.                 case "+":  
  132.                     result = num1 + num2;  
  133.                     txtDisplay.Text = result.ToString();  
  134.                     break;  
  135.                 case "-":  
  136.                     result = num1 - num2;  
  137.                     txtDisplay.Text = result.ToString();  
  138.                     break;  
  139.                 case "x":  
  140.                     result = num1 * num2;  
  141.                     txtDisplay.Text = result.ToString();  
  142.                     break;  
  143.                 case "/":  
  144.                     if (num2 == 0)  
  145.                     {  
  146.                         MessageBox.Show(num1 + " cannot be divided by zero""Error", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  147.                         txtDisplay.Text = "0";  
  148.                     }  
  149.                     else  
  150.                     {  
  151.                         result = num1 / num2;  
  152.                         txtDisplay.Text = result.ToString();  
  153.                     }  
  154.                     break;  
  155.                 case "%":  
  156.                     result = num1 % num2;  
  157.                     txtDisplay.Text = result.ToString();  
  158.                     break;  
  159.             }  
  160.         }  
  161.         //x^2  
  162.         private void btnXsquare_Click(object sender, EventArgs e)  
  163.         {  
  164.             result = decimal.Parse(txtDisplay.Text) * decimal.Parse(txtDisplay.Text);  
  165.             textBox1.Text = txtDisplay.Text + " ^ " + 2 + " ";  
  166.             txtDisplay.Text = result.ToString();  
  167.         }  
  168.         //%  
  169.         private void btnPercentage_Click(object sender, EventArgs e)  
  170.         {  
  171.             num1 = decimal.Parse(txtDisplay.Text);  
  172.             oper = "%";  
  173.             txtDisplay.Text = string.Empty;  
  174.             textBox1.Text = num1.ToString() + " % ";  
  175.         }  
  176.         private void btnErease_Click(object sender, EventArgs e)  
  177.         {  
  178.             string str1 = txtDisplay.Text;  
  179.             int n = str1.Length;  
  180.             txtDisplay.Text = (str1.Substring(0, n - 1));  
  181.         }  
  182.         private void btnC_Click(object sender, EventArgs e)  
  183.         {  
  184.             txtDisplay.Text = string.Empty;  
  185.             textBox1.Text = string.Empty;  
  186.         }  
  187.         private void btnCE_Click(object sender, EventArgs e)  
  188.         {  
  189.             txtDisplay.Text = "0";  
  190.             textBox1.Text = string.Empty;  
  191.         }  
  192.         // dragg form  
  193.         public  
  194.         const int WM_NCLBUTTONDOWN = 0xA1;  
  195.         public  
  196.         const int HT_CAPTION = 0x2;  
  197.         [DllImportAttribute("user32.dll")]  
  198.         public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);  
  199.         [DllImportAttribute("user32.dll")]  
  200.         public static extern bool ReleaseCapture();  
  201.         private void topPanel_MouseDown(object sender, MouseEventArgs e)  
  202.         {  
  203.             ReleaseCapture();  
  204.             SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);  
  205.         }  
  206.         private void btn1x_Click(object sender, EventArgs e)  
  207.         {  
  208.             result = 1 / decimal.Parse(txtDisplay.Text);  
  209.             textBox1.Text = 1 + " / " + txtDisplay.Text + " ";  
  210.             txtDisplay.Text = result.ToString();  
  211.         }  
  212.         private void btnV_Click(object sender, EventArgs e)  
  213.         {  
  214.             double num = Math.Sqrt(double.Parse(txtDisplay.Text));  
  215.             textBox1.Text = "√ " + txtDisplay.Text;  
  216.             result = Convert.ToDecimal(num);  
  217.             txtDisplay.Text = result.ToString();  
  218.         }  
  219.         private void btnClose_Click(object sender, EventArgs e)  
  220.         {  
  221.             Application.Exit();  
  222.         }  
  223.         private void btnMin_Click(object sender, EventArgs e)  
  224.         {  
  225.             this.WindowState = FormWindowState.Minimized;  
  226.         }  
  227.         //..  
  228.     }  
  229. }  
  230. //Author: Bruno Kiafuka