Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. namespace Calculator
  2. {
  3. public partial class Form1 : Form
  4. {
  5. string text = "0";
  6. double firstNumber;
  7. string operation;
  8. public Form1()
  9. {
  10. InitializeComponent();
  11. }
  12.  
  13. private void numButtonClick(object sender, EventArgs e)
  14. {
  15. Button button = (Button)sender;
  16. if(textBox1.Text == "0" && textBox1.Text != null)
  17. {
  18. textBox1.Text = button.Text;
  19. text = button.Text;
  20. }
  21. else
  22. {
  23. textBox1.Text += button.Text;
  24. text = button.Text;
  25. }
  26. }
  27.  
  28. private void button11_Click(object sender, EventArgs e)
  29. {
  30. firstNumber = Convert.ToDouble(text);
  31. textBox1.Text += "+";
  32. text = "0";
  33. operation = "+";
  34. }
  35.  
  36. private void button12_Click(object sender, EventArgs e)
  37. {
  38. firstNumber = Convert.ToDouble(text);
  39. textBox1.Text += "-";
  40. text = "0";
  41. operation = "-";
  42. }
  43.  
  44. private void button13_Click(object sender, EventArgs e)
  45. {
  46. firstNumber = Convert.ToDouble(text);
  47. textBox1.Text += "x";
  48. text = "0";
  49. operation = "x";
  50. }
  51.  
  52. private void button14_Click(object sender, EventArgs e)
  53. {
  54. firstNumber = Convert.ToDouble(text);
  55. textBox1.Text += "/";
  56. text = "0";
  57. operation = "/";
  58. }
  59.  
  60. private void button15_Click(object sender, EventArgs e)
  61. {
  62. textBox1.Text = "0";
  63. text = "0";
  64. firstNumber = 0;
  65. }
  66.  
  67. private void button16_Click(object sender, EventArgs e)
  68. {
  69. textBox1.Text += ",";
  70. }
  71.  
  72. private void button18_Click(object sender, EventArgs e)
  73. {
  74. textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
  75. text = text.Remove(text.Length - 1);
  76. }
  77.  
  78. private void button17_Click(object sender, EventArgs e)
  79. {
  80. double secondNumber = Convert.ToDouble(text);
  81. double result = 0;
  82. if (operation == "+")
  83. {
  84. result = firstNumber + secondNumber;
  85. }
  86. else if (operation == "-")
  87. {
  88. result = firstNumber - secondNumber;
  89. }
  90. else if (operation == "x")
  91. {
  92. result = firstNumber * secondNumber;
  93. }
  94. else if (operation == "/")
  95. {
  96. if (secondNumber == 0)
  97. {
  98. textBox1.Text = "Cannot divide by zero";
  99. }
  100. else
  101. {
  102. result = firstNumber / secondNumber;
  103. }
  104. }
  105.  
  106. textBox1.Text = Convert.ToString(result);
  107. text = Convert.ToString(result);
  108. firstNumber = result;
  109.  
  110. }
  111.  
  112. private void textBox1_TextChanged(object sender, EventArgs e)
  113. {
  114.  
  115. }
  116. }
  117. }