Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Calculator_Project
- {
- public partial class Calculator : Form
- {
- public Calculator()
- {
- InitializeComponent();
- }
- //The Add Button
- private void Addbtn_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(TextboxOne.Text))
- {
- MessageBox.Show("Please enter a value in Textbox 1 !");
- return;
- }
- if (string.IsNullOrEmpty(TextboxTwo.Text))
- {
- MessageBox.Show("Please enter a value in Textbox 2!");
- return;
- }
- //int.Parse converts string to an integer form,
- double number1 = int.Parse(TextboxOne.Text );
- double number2 = int.Parse(TextboxTwo.Text);
- double answer;
- //This is will work out the answer in the 'Add Button' using the '+' operator.
- answer = number1 + number2;
- //The ResultBox.Text is the display in which the Answer is. The answer is worked out in the code containg the '+' operator.
- ResultBox.Text = answer.ToString();
- }
- //The Subtract Button
- private void Subbtn_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(TextboxOne.Text))
- {
- MessageBox.Show("Please enter a value in Textbox 1!");
- return;
- }
- if (string.IsNullOrEmpty(TextboxTwo.Text))
- {
- MessageBox.Show("Please enter a value in Textbox 2!");
- return;
- }
- double number1 = int.Parse(TextboxOne.Text);
- double number2 = int.Parse(TextboxTwo.Text);
- double answer;
- //This piece of code will work out the answer in the 'Sub Button' using the '-' operator.
- answer = number1 - number2;
- ResultBox.Text = answer.ToString();
- }
- //The Multiply Button
- private void Multbtn_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(TextboxOne.Text))
- {
- MessageBox.Show("Please enter a value in Textbox 1!");
- return;
- }
- if (string.IsNullOrEmpty(TextboxTwo.Text))
- {
- MessageBox.Show("Please enter a value in Textbox 2!");
- return;
- }
- double number1 = int.Parse(TextboxOne.Text);
- double number2 = int.Parse(TextboxTwo.Text);
- double answer;
- //This is will work out the answer in the 'Mult Button' using the '*' operator.
- answer = number1 * number2;
- ResultBox.Text = answer.ToString();
- }
- //The Divide Button
- private void Divbtn_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrWhiteSpace(TextboxOne.Text))
- {
- MessageBox.Show("Please enter a value in Textbox 1!");
- return;
- }
- if (string.IsNullOrWhiteSpace(TextboxTwo.Text))
- {
- MessageBox.Show("Please enter a value in Textbox 2!");
- return;
- }
- double number1 = int.Parse(TextboxOne.Text);
- double number2 = int.Parse(TextboxTwo.Text);
- double answer;
- if (TextboxOne.Text == "0" || TextboxTwo.Text == "0")
- {
- MessageBox.Show("This is a divison with 0 which cannot be carried out!");
- return;
- }
- if (number2 !=0)
- {
- //This is will work out the answer in the 'Div Button' using the '/' operator.
- answer = number1 / number2;
- ResultBox.Text = answer.ToString();
- }
- else
- {
- MessageBox.Show("This is a division by 0 which cannot be carried out!");
- return;
- }
- }
- //The clear Button
- private void Clearbtn_Click(object sender, EventArgs e)
- {
- //The operations sets the TextBox texts to 'nothing'.
- TextboxOne.Text = "";
- TextboxTwo.Text = "";
- ResultBox.Text = "";
- }
- private void TextboxOne_KeyPress(object sender, KeyPressEventArgs e)
- {
- //This piece of code prevents the user from typing letters int the text box at all.
- if (char.IsNumber(e.KeyChar))
- {
- }
- else
- {
- e.Handled = e.KeyChar != (char)Keys.Back;
- }
- }
- private void TextboxTwo_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (char.IsNumber(e.KeyChar))
- {
- }
- else
- {
- e.Handled = e.KeyChar != (char)Keys.Back;
- }
- }
- private void Calculator_Load(object sender, EventArgs e)
- {
- }
- private void TextboxOne_TextChanged(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement