Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  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.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11.  
  12. namespace Calculator_Project
  13. {
  14. public partial class Calculator : Form
  15. {
  16. public Calculator()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21.  
  22. //The Add Button
  23. private void Addbtn_Click(object sender, EventArgs e)
  24. {
  25. if (string.IsNullOrEmpty(TextboxOne.Text))
  26. {
  27. MessageBox.Show("Please enter a value in Textbox 1 !");
  28. return;
  29. }
  30.  
  31. if (string.IsNullOrEmpty(TextboxTwo.Text))
  32. {
  33. MessageBox.Show("Please enter a value in Textbox 2!");
  34. return;
  35. }
  36.  
  37. //int.Parse converts string to an integer form,
  38. double number1 = int.Parse(TextboxOne.Text );
  39. double number2 = int.Parse(TextboxTwo.Text);
  40. double answer;
  41.  
  42.  
  43.  
  44. //This is will work out the answer in the 'Add Button' using the '+' operator.
  45. answer = number1 + number2;
  46.  
  47.  
  48. //The ResultBox.Text is the display in which the Answer is. The answer is worked out in the code containg the '+' operator.
  49. ResultBox.Text = answer.ToString();
  50.  
  51.  
  52. }
  53.  
  54. //The Subtract Button
  55. private void Subbtn_Click(object sender, EventArgs e)
  56. {
  57.  
  58.  
  59. if (string.IsNullOrEmpty(TextboxOne.Text))
  60. {
  61. MessageBox.Show("Please enter a value in Textbox 1!");
  62. return;
  63. }
  64.  
  65. if (string.IsNullOrEmpty(TextboxTwo.Text))
  66. {
  67. MessageBox.Show("Please enter a value in Textbox 2!");
  68. return;
  69. }
  70.  
  71. double number1 = int.Parse(TextboxOne.Text);
  72. double number2 = int.Parse(TextboxTwo.Text);
  73. double answer;
  74.  
  75. //This piece of code will work out the answer in the 'Sub Button' using the '-' operator.
  76. answer = number1 - number2;
  77.  
  78. ResultBox.Text = answer.ToString();
  79.  
  80.  
  81. }
  82.  
  83. //The Multiply Button
  84. private void Multbtn_Click(object sender, EventArgs e)
  85. {
  86.  
  87. if (string.IsNullOrEmpty(TextboxOne.Text))
  88. {
  89. MessageBox.Show("Please enter a value in Textbox 1!");
  90. return;
  91. }
  92.  
  93. if (string.IsNullOrEmpty(TextboxTwo.Text))
  94. {
  95. MessageBox.Show("Please enter a value in Textbox 2!");
  96. return;
  97. }
  98.  
  99. double number1 = int.Parse(TextboxOne.Text);
  100. double number2 = int.Parse(TextboxTwo.Text);
  101. double answer;
  102.  
  103.  
  104.  
  105. //This is will work out the answer in the 'Mult Button' using the '*' operator.
  106. answer = number1 * number2;
  107.  
  108. ResultBox.Text = answer.ToString();
  109.  
  110.  
  111. }
  112.  
  113. //The Divide Button
  114.  
  115. private void Divbtn_Click(object sender, EventArgs e)
  116. {
  117.  
  118. if (string.IsNullOrWhiteSpace(TextboxOne.Text))
  119. {
  120. MessageBox.Show("Please enter a value in Textbox 1!");
  121. return;
  122. }
  123.  
  124. if (string.IsNullOrWhiteSpace(TextboxTwo.Text))
  125. {
  126. MessageBox.Show("Please enter a value in Textbox 2!");
  127. return;
  128. }
  129.  
  130.  
  131.  
  132.  
  133. double number1 = int.Parse(TextboxOne.Text);
  134. double number2 = int.Parse(TextboxTwo.Text);
  135. double answer;
  136.  
  137.  
  138. if (TextboxOne.Text == "0" || TextboxTwo.Text == "0")
  139. {
  140. MessageBox.Show("This is a divison with 0 which cannot be carried out!");
  141. return;
  142. }
  143.  
  144. if (number2 !=0)
  145. {
  146. //This is will work out the answer in the 'Div Button' using the '/' operator.
  147. answer = number1 / number2;
  148. ResultBox.Text = answer.ToString();
  149. }
  150. else
  151. {
  152. MessageBox.Show("This is a division by 0 which cannot be carried out!");
  153. return;
  154. }
  155.  
  156.  
  157. }
  158.  
  159. //The clear Button
  160. private void Clearbtn_Click(object sender, EventArgs e)
  161. {
  162. //The operations sets the TextBox texts to 'nothing'.
  163. TextboxOne.Text = "";
  164. TextboxTwo.Text = "";
  165. ResultBox.Text = "";
  166. }
  167.  
  168. private void TextboxOne_KeyPress(object sender, KeyPressEventArgs e)
  169. {
  170. //This piece of code prevents the user from typing letters int the text box at all.
  171. if (char.IsNumber(e.KeyChar))
  172. {
  173.  
  174.  
  175. }
  176. else
  177. {
  178. e.Handled = e.KeyChar != (char)Keys.Back;
  179. }
  180. }
  181.  
  182. private void TextboxTwo_KeyPress(object sender, KeyPressEventArgs e)
  183. {
  184. if (char.IsNumber(e.KeyChar))
  185. {
  186.  
  187.  
  188. }
  189. else
  190. {
  191. e.Handled = e.KeyChar != (char)Keys.Back;
  192. }
  193. }
  194.  
  195. private void Calculator_Load(object sender, EventArgs e)
  196. {
  197.  
  198. }
  199.  
  200. private void TextboxOne_TextChanged(object sender, EventArgs e)
  201. {
  202.  
  203. }
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement