My objective is to create a new class, Calculate.cs, to do all the calculations for my calculator program.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SimpleCalculatorProgram
{
public partial class Form1 : Form
{
public int intNumber1;
private int intNumber2;
private char chOperator;
private Calculate calculate;
private Validator validator;
public Form1()
{
InitializeComponent();
validator = new Validator();
calculate = new Calculate();
btnCalculate.Enabled = false;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtNumber1.Text = String.Empty;
txtNumber2.Text = String.Empty;
txtOperator.Text = String.Empty;
lblResult.Text = String.Empty;
btnCalculate.Enabled = false;
txtNumber1.Focus();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
int intResult = 0;
getUserInput();
switch (chOperator)
{
case '+':
intResult = intNumber1 + intNumber2;
break;
case '-':
intResult = intNumber1 - intNumber2;
break;
case '*':
intResult = intNumber1 * intNumber2;
break;
case '/':
intResult = intNumber1 / intNumber2;
break;
default:
MessageBox.Show("Incorrect entry; please try again!");
break;
}
displayResult(intResult);
}
private void getUserInput()
{
intNumber1 = Convert.ToInt32(txtNumber1.Text);
intNumber2 = Convert.ToInt32(txtNumber2.Text);
chOperator = Convert.ToChar(txtOperator.Text);
}
private void displayResult(int computedResult)
{
lblResult.Text = Convert.ToString(computedResult);
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
//Event handlers created when the textbox property 'CausesValidation' is set to true
private void txtNumber_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
this.btnCalculate.Enabled = true;
String inputText = ((TextBox)sender).Text;
//Argument below calls the HasInput() method of the Validator class for the
//object named validator, passing the String value stored in inputText
if (!validator.HasInput(inputText))
{
MessageBox.Show("Please enter a value.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
//Cancels the current event handler execution
e.Cancel = true;
}
else
{
//Argument below calls the IsNumeric() method of the Validator class for the
//object named validator, passing the String value stored in the inputText
if (!validator.IsNumeric(inputText))
{
MessageBox.Show("Please enter an integer.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
e.Cancel = true;
}
}
}
private void txtOperator_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
String inputText = ((TextBox)sender).Text.Trim();
if (!validator.HasInput(inputText))
{
MessageBox.Show("Please enter a value.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
e.Cancel = true;
}
else
{
if (!validator.IsOperator(inputText))
{
MessageBox.Show("Please enter a valid operator: +,-,*,/.", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning);
e.Cancel = true;
}
}
}
}
}
How do I start to remove the code from the existing code and made the Calculate class? What steps do I make? Are their any good tutorials or helpful readings? Could you provide me with some examples? Could you possibly help me start the code? Any advice would be greatly appreciated!
Thank you very much in advance!

New Topic/Question
Reply
MultiQuote










|