C# Program . Create a Windows application that has the functionality of a calcul
ID: 3701473 • Letter: C
Question
C# Program. Create a Windows application that has the functionality of a calculator and works with integral values. Allow the user to select buttons representing numeric values. If the user attempts to divide by zero, throw and handle an exception.
Add these changes to the program.
Create a Windows Form application that will have two text boxes that accept two INTEGERS, and allow for division, multiplication, addition, and subtraction.
Your program must do exception handling to prevent either division by zero and improper number entry. Do not just check the number and correct (i.e. do not use TryParse this time) -- you must use try/catch blocks to do the work.
Also, the program must use an instantiated object to do the math and return both the results of the math and the success or failure (error message).
I am completely lost on how to do this. Commenting the code would be appreciated so I can understand the process of how to do this.
Thank you!!
Explanation / Answer
Please find my implementation:
below is the code from the windows application in visual studio:
Form1.cs
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 SimpleCalculator
{
public partial class frmCalculator : Form
{
string operand1 = string.Empty;
string operand2 = string.Empty;
string result;
char operation;
public frmCalculator()
{
InitializeComponent();
}
private void frmCalculator_Load(object sender, EventArgs e)
{
btnOne.Click += new EventHandler(btn_Click);
btnTwo.Click += new EventHandler(btn_Click);
btnThree.Click += new EventHandler(btn_Click);
btnFour.Click += new EventHandler(btn_Click);
btnFive.Click += new EventHandler(btn_Click);
btnSix.Click += new EventHandler(btn_Click);
btnSeven.Click += new EventHandler(btn_Click);
btnEight.Click += new EventHandler(btn_Click);
btnNine.Click += new EventHandler(btn_Click);
btnZero.Click += new EventHandler(btn_Click);
btnDot.Click += new EventHandler(btn_Click);
}
void btn_Click(object sender, EventArgs e)
{
try
{
Button btn = sender as Button;
switch (btn.Name)
{
case "btnOne":
txtInput.Text += "1";
break;
case "btnTwo":
txtInput.Text += "2";
break;
case "btnThree":
txtInput.Text += "3";
break;
case "btnFour":
txtInput.Text += "4";
break;
case "btnFive":
txtInput.Text += "5";
break;
case "btnSix":
txtInput.Text += "6";
break;
case "btnSeven":
txtInput.Text += "7";
break;
case "btnEight":
txtInput.Text += "8";
break;
case "btnNine":
txtInput.Text += "9";
break;
case "btnZero":
txtInput.Text += "0";
break;
case "btnDot":
if(!txtInput.Text.Contains("."))
txtInput.Text += ".";
break;
}
}
catch(Exception ex)
{
MessageBox.Show("Sorry for the inconvenience, Unexpected error occured. Details: " +
ex.Message);
}
}
private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
//case '+':
//case '-':
//case '*':
//case '/':
//case '.':
break;
default:
e.Handled = true;
MessageBox.Show("Only numbers, +, -, ., *, / are allowed");
break;
}
}
private void txtInput_TextChanged(object sender, EventArgs e)
{
}
private void btnPlus_Click(object sender, EventArgs e)
{
operand1 = txtInput.Text;
operation = '+';
txtInput.Text = string.Empty;
}
private void btnMinus_Click(object sender, EventArgs e)
{
operand1 = txtInput.Text;
operation = '-';
txtInput.Text = string.Empty;
}
private void btnMulitply_Click(object sender, EventArgs e)
{
operand1 = txtInput.Text;
operation = '*';
txtInput.Text = string.Empty;
}
private void btnDivide_Click(object sender, EventArgs e)
{
operand1 = txtInput.Text;
operation = '/';
txtInput.Text = string.Empty;
}
private void btnEqual_Click(object sender, EventArgs e)
{
operand2 = txtInput.Text;
double opr1, opr2;
double.TryParse(operand1, out opr1);
double.TryParse(operand2, out opr2);
switch (operation)
{
case '+':
result = (opr1 + opr2).ToString();
break;
case '-':
result = (opr1 - opr2).ToString();
break;
case '*':
result = (opr1 * opr2).ToString();
break;
case '/':
if (opr2 != 0)
{
result = (opr1 / opr2).ToString();
}
else
{
MessageBox.Show("Can't divide by zero");
}
break;
}
txtInput.Text = result.ToString();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtInput.Text = string.Empty;
operand1 = string.Empty;
operand2 = string.Empty;
}
private void btnSqrRoot_Click(object sender, EventArgs e)
{
double opr1;
if (double.TryParse(txtInput.Text, out opr1))
{
txtInput.Text = (Math.Sqrt(opr1)).ToString();
}
}
private void btnByTwo_Click(object sender, EventArgs e)
{
double opr1;
if (double.TryParse(txtInput.Text, out opr1))
{
txtInput.Text = (opr1 / 2).ToString();
}
}
private void btnByFour_Click(object sender, EventArgs e)
{
double opr1;
if (double.TryParse(txtInput.Text, out opr1))
{
txtInput.Text = (opr1 / 4).ToString();
}
}
}
}
Form1.Designer.cs
namespace SimpleCalculator
{
partial class frmCalculator
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtInput = new System.Windows.Forms.TextBox();
this.btnOne = new System.Windows.Forms.Button();
this.btnTwo = new System.Windows.Forms.Button();
this.btnThree = new System.Windows.Forms.Button();
this.btnFour = new System.Windows.Forms.Button();
this.btnFive = new System.Windows.Forms.Button();
this.btnSix = new System.Windows.Forms.Button();
this.btnSeven = new System.Windows.Forms.Button();
this.btnEight = new System.Windows.Forms.Button();
this.btnNine = new System.Windows.Forms.Button();
this.btnZero = new System.Windows.Forms.Button();
this.btnDot = new System.Windows.Forms.Button();
this.btnEqual = new System.Windows.Forms.Button();
this.btnPlus = new System.Windows.Forms.Button();
this.btnMinus = new System.Windows.Forms.Button();
this.btnMulitply = new System.Windows.Forms.Button();
this.btnDivide = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.btnSqrRoot = new System.Windows.Forms.Button();
this.btnByTwo = new System.Windows.Forms.Button();
this.btnByFour = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(13, 13);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(193, 20);
this.txtInput.TabIndex = 0;
this.txtInput.TextChanged += new System.EventHandler(this.txtInput_TextChanged);
this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtInput_KeyPress);
//
// btnOne
//
this.btnOne.Location = new System.Drawing.Point(12, 39);
this.btnOne.Name = "btnOne";
this.btnOne.Size = new System.Drawing.Size(34, 23);
this.btnOne.TabIndex = 1;
this.btnOne.Text = "1";
this.btnOne.UseVisualStyleBackColor = true;
//
// btnTwo
//
this.btnTwo.Location = new System.Drawing.Point(52, 39);
this.btnTwo.Name = "btnTwo";
this.btnTwo.Size = new System.Drawing.Size(34, 23);
this.btnTwo.TabIndex = 2;
this.btnTwo.Text = "2";
this.btnTwo.UseVisualStyleBackColor = true;
//
// btnThree
//
this.btnThree.Location = new System.Drawing.Point(92, 39);
this.btnThree.Name = "btnThree";
this.btnThree.Size = new System.Drawing.Size(34, 23);
this.btnThree.TabIndex = 3;
this.btnThree.Text = "3";
this.btnThree.UseVisualStyleBackColor = true;
//
// btnFour
//
this.btnFour.Location = new System.Drawing.Point(12, 68);
this.btnFour.Name = "btnFour";
this.btnFour.Size = new System.Drawing.Size(34, 23);
this.btnFour.TabIndex = 4;
this.btnFour.Text = "4";
this.btnFour.UseVisualStyleBackColor = true;
//
// btnFive
//
this.btnFive.Location = new System.Drawing.Point(52, 68);
this.btnFive.Name = "btnFive";
this.btnFive.Size = new System.Drawing.Size(34, 23);
this.btnFive.TabIndex = 5;
this.btnFive.Text = "5";
this.btnFive.UseVisualStyleBackColor = true;
//
// btnSix
//
this.btnSix.Location = new System.Drawing.Point(92, 68);
this.btnSix.Name = "btnSix";
this.btnSix.Size = new System.Drawing.Size(34, 23);
this.btnSix.TabIndex = 6;
this.btnSix.Text = "6";
this.btnSix.UseVisualStyleBackColor = true;
//
// btnSeven
//
this.btnSeven.Location = new System.Drawing.Point(13, 97);
this.btnSeven.Name = "btnSeven";
this.btnSeven.Size = new System.Drawing.Size(34, 23);
this.btnSeven.TabIndex = 7;
this.btnSeven.Text = "7";
this.btnSeven.UseVisualStyleBackColor = true;
//
// btnEight
//
this.btnEight.Location = new System.Drawing.Point(53, 97);
this.btnEight.Name = "btnEight";
this.btnEight.Size = new System.Drawing.Size(34, 23);
this.btnEight.TabIndex = 8;
this.btnEight.Text = "8";
this.btnEight.UseVisualStyleBackColor = true;
//
// btnNine
//
this.btnNine.Location = new System.Drawing.Point(93, 97);
this.btnNine.Name = "btnNine";
this.btnNine.Size = new System.Drawing.Size(34, 23);
this.btnNine.TabIndex = 9;
this.btnNine.Text = "9";
this.btnNine.UseVisualStyleBackColor = true;
//
// btnZero
//
this.btnZero.Location = new System.Drawing.Point(13, 126);
this.btnZero.Name = "btnZero";
this.btnZero.Size = new System.Drawing.Size(34, 23);
this.btnZero.TabIndex = 10;
this.btnZero.Text = "0";
this.btnZero.UseVisualStyleBackColor = true;
//
// btnDot
//
this.btnDot.Location = new System.Drawing.Point(52, 126);
this.btnDot.Name = "btnDot";
this.btnDot.Size = new System.Drawing.Size(34, 23);
this.btnDot.TabIndex = 11;
this.btnDot.Text = ".";
this.btnDot.UseVisualStyleBackColor = true;
//
// btnEqual
//
this.btnEqual.Location = new System.Drawing.Point(93, 126);
this.btnEqual.Name = "btnEqual";
this.btnEqual.Size = new System.Drawing.Size(34, 23);
this.btnEqual.TabIndex = 12;
this.btnEqual.Text = "=";
this.btnEqual.UseVisualStyleBackColor = true;
this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click);
//
// btnPlus
//
this.btnPlus.Location = new System.Drawing.Point(132, 39);
this.btnPlus.Name = "btnPlus";
this.btnPlus.Size = new System.Drawing.Size(34, 23);
this.btnPlus.TabIndex = 13;
this.btnPlus.Text = "+";
this.btnPlus.UseVisualStyleBackColor = true;
this.btnPlus.Click += new System.EventHandler(this.btnPlus_Click);
//
// btnMinus
//
this.btnMinus.Location = new System.Drawing.Point(132, 68);
this.btnMinus.Name = "btnMinus";
this.btnMinus.Size = new System.Drawing.Size(34, 23);
this.btnMinus.TabIndex = 14;
this.btnMinus.Text = "-";
this.btnMinus.UseVisualStyleBackColor = true;
this.btnMinus.Click += new System.EventHandler(this.btnMinus_Click);
//
// btnMulitply
//
this.btnMulitply.Location = new System.Drawing.Point(132, 97);
this.btnMulitply.Name = "btnMulitply";
this.btnMulitply.Size = new System.Drawing.Size(34, 23);
this.btnMulitply.TabIndex = 15;
this.btnMulitply.Text = "*";
this.btnMulitply.UseVisualStyleBackColor = true;
this.btnMulitply.Click += new System.EventHandler(this.btnMulitply_Click);
//
// btnDivide
//
this.btnDivide.Location = new System.Drawing.Point(132, 126);
this.btnDivide.Name = "btnDivide";
this.btnDivide.Size = new System.Drawing.Size(34, 23);
this.btnDivide.TabIndex = 16;
this.btnDivide.Text = "/";
this.btnDivide.UseVisualStyleBackColor = true;
this.btnDivide.Click += new System.EventHandler(this.btnDivide_Click);
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(172, 39);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(34, 23);
this.btnClear.TabIndex = 17;
this.btnClear.Text = "C";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// btnSqrRoot
//
this.btnSqrRoot.Location = new System.Drawing.Point(172, 68);
this.btnSqrRoot.Name = "btnSqrRoot";
this.btnSqrRoot.Size = new System.Drawing.Size(34, 23);
this.btnSqrRoot.TabIndex = 18;
this.btnSqrRoot.Text = "?";
this.btnSqrRoot.UseVisualStyleBackColor = true;
this.btnSqrRoot.Click += new System.EventHandler(this.btnSqrRoot_Click);
//
// btnByTwo
//
this.btnByTwo.Location = new System.Drawing.Point(172, 97);
this.btnByTwo.Name = "btnByTwo";
this.btnByTwo.Size = new System.Drawing.Size(34, 23);
this.btnByTwo.TabIndex = 19;
this.btnByTwo.Text = "½";
this.btnByTwo.UseVisualStyleBackColor = true;
this.btnByTwo.Click += new System.EventHandler(this.btnByTwo_Click);
//
// btnByFour
//
this.btnByFour.Location = new System.Drawing.Point(172, 126);
this.btnByFour.Name = "btnByFour";
this.btnByFour.Size = new System.Drawing.Size(34, 23);
this.btnByFour.TabIndex = 20;
this.btnByFour.Text = "¼";
this.btnByFour.UseVisualStyleBackColor = true;
this.btnByFour.Click += new System.EventHandler(this.btnByFour_Click);
//
// frmCalculator
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(214, 157);
this.Controls.Add(this.btnByFour);
this.Controls.Add(this.btnByTwo);
this.Controls.Add(this.btnSqrRoot);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnDivide);
this.Controls.Add(this.btnMulitply);
this.Controls.Add(this.btnMinus);
this.Controls.Add(this.btnPlus);
this.Controls.Add(this.btnEqual);
this.Controls.Add(this.btnDot);
this.Controls.Add(this.btnZero);
this.Controls.Add(this.btnNine);
this.Controls.Add(this.btnEight);
this.Controls.Add(this.btnSeven);
this.Controls.Add(this.btnSix);
this.Controls.Add(this.btnFive);
this.Controls.Add(this.btnFour);
this.Controls.Add(this.btnThree);
this.Controls.Add(this.btnTwo);
this.Controls.Add(this.btnOne);
this.Controls.Add(this.txtInput);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frmCalculator";
this.Text = "Simple Calculator";
this.Load += new System.EventHandler(this.frmCalculator_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.Button btnOne;
private System.Windows.Forms.Button btnTwo;
private System.Windows.Forms.Button btnThree;
private System.Windows.Forms.Button btnFour;
private System.Windows.Forms.Button btnFive;
private System.Windows.Forms.Button btnSix;
private System.Windows.Forms.Button btnSeven;
private System.Windows.Forms.Button btnEight;
private System.Windows.Forms.Button btnNine;
private System.Windows.Forms.Button btnZero;
private System.Windows.Forms.Button btnDot;
private System.Windows.Forms.Button btnEqual;
private System.Windows.Forms.Button btnPlus;
private System.Windows.Forms.Button btnMinus;
private System.Windows.Forms.Button btnMulitply;
private System.Windows.Forms.Button btnDivide;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnSqrRoot;
private System.Windows.Forms.Button btnByTwo;
private System.Windows.Forms.Button btnByFour;
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace SimpleCalculator
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmCalculator());
}
}
}
feel free to ask if you have any doubt :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.