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

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.

I have developed the code I am receiving the following error CS 1513 } expected

The Code:

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 Calculator2

{

public partial class Form1 : Form

{

Double resultValue = 0;

String operationPerformed = ""; //perform addition,substract, division or muliplication

bool isOperationPerformed = false; //has add, substract,division or muliplication been performed

public Form1()

{

InitializeComponent();

}

private void button_Click(object sender, EventArgs e)

{

if ((txtResult.Text == "0") || (isOperationPerformed))

txtResult.Clear();

isOperationPerformed = false;

Button button = (Button)sender; // tie number buttons to one click (method) event

if (button.Text == ".")

{

if (!txtResult.Text.Contains("."))

txtResult.Text = txtResult.Text + button.Text;

} else

txtResult.Text = txtResult.Text + button.Text;

}

private void operator_click(object sender, EventArgs e)

{

Button button = (Button)sender;//tie operator (+,-,*,/) buttons to one click (method)

if (resultValue != 0)

{

btnEqual.PerformClick();

operationPerformed = button.Text;

lblOperation.Text = resultValue + "" + operationPerformed;

isOperationPerformed = true;

}

else

{

operationPerformed = button.Text;

resultValue = Double.Parse(txtResult.Text);

lblOperation.Text = resultValue + "" + operationPerformed;

isOperationPerformed = true;

}

}

private void btnClearEntry_Click(object sender, EventArgs e)

{

txtResult.Text = "0"; //clear last entry

}

private void btnClear_Click(object sender, EventArgs e)

{

txtResult.Text = "0";//clear last entry

resultValue = 0;//and reset text box reuslt to zero

}

//Perform math operations

private void btnEqual_Click(object sender, EventArgs e)

{

try

{

switch (operationPerformed)

{

case "+":

txtResult.Text = (resultValue + Double.Parse(txtResult.Text)).ToString();

break;

case "-":

txtResult.Text = (resultValue - Double.Parse(txtResult.Text)).ToString();

break;

case "*":

txtResult.Text = (resultValue * Double.Parse(txtResult.Text)).ToString();

break;

case "/":

txtResult.Text = (resultValue / Double.Parse(txtResult.Text)).ToString();

break;

default:

break;

} // user define excpetion

throw new DivideException("Divide by zero" + " is not possible");

// Catch the exception when a usr try to divide a number by zero

catch (DivideException ex)

{

MessageBox.Show(ex.Message);

resultValue = Int32.Parse(txtResult.Text);

operationPerformed = "";

lblOperation.Text = "";

}//end of switch

}// end of btnEqual_Click

}//end of class

}//End of Calculator2

Form Designer

namespace Calculator2

{

partial class Form1

{

///

/// Required designer variable.

///

private System.ComponentModel.IContainer components = null;

///

/// Clean up any resources being used.

///

/// true if managed resources should be disposed; otherwise, false.

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

///

private void InitializeComponent()

{

this.txtResult = new System.Windows.Forms.TextBox();

this.btnSeven = new System.Windows.Forms.Button();

this.button2 = new System.Windows.Forms.Button();

this.btnClearEntry = new System.Windows.Forms.Button();

this.btnDivide = new System.Windows.Forms.Button();

this.btnNine = new System.Windows.Forms.Button();

this.btnEight = new System.Windows.Forms.Button();

this.btnTimes = new System.Windows.Forms.Button();

this.btnClear = new System.Windows.Forms.Button();

this.btn6 = new System.Windows.Forms.Button();

this.btnTwo = new System.Windows.Forms.Button();

this.btnThree = new System.Windows.Forms.Button();

this.btnOne = new System.Windows.Forms.Button();

this.btnZero = new System.Windows.Forms.Button();

this.button14 = new System.Windows.Forms.Button();

this.btnDecimal = new System.Windows.Forms.Button();

this.btnAdd = new System.Windows.Forms.Button();

this.btnSubstract = new System.Windows.Forms.Button();

this.btnFour = new System.Windows.Forms.Button();

this.btnFive = new System.Windows.Forms.Button();

this.lblOperation = new System.Windows.Forms.Label();

this.btnEqual = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// txtResult

//

this.txtResult.BackColor = System.Drawing.SystemColors.ButtonHighlight;

this.txtResult.Location = new System.Drawing.Point(24, 40);

this.txtResult.Name = "txtResult";

this.txtResult.ReadOnly = true;

this.txtResult.Size = new System.Drawing.Size(326, 22);

this.txtResult.TabIndex = 0;

this.txtResult.Text = "0";

this.txtResult.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;

//

// btnSeven

//

this.btnSeven.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

this.btnSeven.Location = new System.Drawing.Point(24, 113);

this.btnSeven.Name = "btnSeven";

this.btnSeven.Size = new System.Drawing.Size(54, 23);

this.btnSeven.TabIndex = 1;

this.btnSeven.Text = "7";

this.btnSeven.UseVisualStyleBackColor = true;

this.btnSeven.Click += new System.EventHandler(this.button_Click);

//

// button2

//

this.button2.Location = new System.Drawing.Point(134, 209);

this.button2.Name = "button2";

this.button2.Size = new System.Drawing.Size(8, 8);

this.button2.TabIndex = 2;

this.button2.Text = "button2";

this.button2.UseVisualStyleBackColor = true;

//

// btnClearEntry

//

this.btnClearEntry.Location = new System.Drawing.Point(313, 113);

this.btnClearEntry.Name = "btnClearEntry";

this.btnClearEntry.Size = new System.Drawing.Size(54, 23);

this.btnClearEntry.TabIndex = 3;

this.btnClearEntry.Text = "CE";

this.btnClearEntry.UseVisualStyleBackColor = true;

this.btnClearEntry.Click += new System.EventHandler(this.btnClearEntry_Click);

//

// btnDivide

//

this.btnDivide.Location = new System.Drawing.Point(236, 114);

this.btnDivide.Name = "btnDivide";

this.btnDivide.Size = new System.Drawing.Size(54, 23);

this.btnDivide.TabIndex = 4;

this.btnDivide.Text = "/";

this.btnDivide.UseVisualStyleBackColor = true;

this.btnDivide.Click += new System.EventHandler(this.operator_click);

//

// btnNine

//

this.btnNine.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

this.btnNine.Location = new System.Drawing.Point(165, 113);

this.btnNine.Name = "btnNine";

this.btnNine.Size = new System.Drawing.Size(54, 23);

this.btnNine.TabIndex = 5;

this.btnNine.Text = "9";

this.btnNine.UseVisualStyleBackColor = true;

this.btnNine.Click += new System.EventHandler(this.button_Click);

//

// btnEight

//

this.btnEight.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

this.btnEight.Location = new System.Drawing.Point(88, 113);

this.btnEight.Name = "btnEight";

this.btnEight.Size = new System.Drawing.Size(54, 23);

this.btnEight.TabIndex = 6;

this.btnEight.Text = "8";

this.btnEight.UseVisualStyleBackColor = true;

this.btnEight.Click += new System.EventHandler(this.button_Click);

//

// btnTimes

//

this.btnTimes.Location = new System.Drawing.Point(236, 159);

this.btnTimes.Name = "btnTimes";

this.btnTimes.Size = new System.Drawing.Size(54, 23);

this.btnTimes.TabIndex = 9;

this.btnTimes.Text = "*";

this.btnTimes.UseVisualStyleBackColor = true;

this.btnTimes.Click += new System.EventHandler(this.operator_click);

//

// btnClear

//

this.btnClear.Location = new System.Drawing.Point(313, 159);

this.btnClear.Name = "btnClear";

this.btnClear.Size = new System.Drawing.Size(54, 23);

this.btnClear.TabIndex = 8;

this.btnClear.Text = "C";

this.btnClear.UseVisualStyleBackColor = true;

this.btnClear.Click += new System.EventHandler(this.btnClear_Click);

//

// btn6

//

this.btn6.Location = new System.Drawing.Point(165, 159);

this.btn6.Name = "btn6";

this.btn6.Size = new System.Drawing.Size(54, 23);

this.btn6.TabIndex = 7;

this.btn6.Text = "6";

this.btn6.UseVisualStyleBackColor = true;

this.btn6.Click += new System.EventHandler(this.button_Click);

//

// btnTwo

//

this.btnTwo.Location = new System.Drawing.Point(88, 202);

this.btnTwo.Name = "btnTwo";

this.btnTwo.Size = new System.Drawing.Size(54, 23);

this.btnTwo.TabIndex = 12;

this.btnTwo.Text = "2";

this.btnTwo.UseVisualStyleBackColor = true;

this.btnTwo.Click += new System.EventHandler(this.button_Click);

//

// btnThree

//

this.btnThree.Location = new System.Drawing.Point(165, 202);

this.btnThree.Name = "btnThree";

this.btnThree.Size = new System.Drawing.Size(54, 23);

this.btnThree.TabIndex = 11;

this.btnThree.Text = "3";

this.btnThree.UseVisualStyleBackColor = true;

this.btnThree.Click += new System.EventHandler(this.button_Click);

//

// btnOne

//

this.btnOne.Location = new System.Drawing.Point(24, 202);

this.btnOne.Name = "btnOne";

this.btnOne.Size = new System.Drawing.Size(54, 23);

this.btnOne.TabIndex = 10;

this.btnOne.Text = "1";

this.btnOne.UseVisualStyleBackColor = true;

this.btnOne.Click += new System.EventHandler(this.button_Click);

//

// btnZero

//

this.btnZero.Location = new System.Drawing.Point(88, 251);

this.btnZero.Name = "btnZero";

this.btnZero.Size = new System.Drawing.Size(54, 23);

this.btnZero.TabIndex = 15;

this.btnZero.Text = "0";

this.btnZero.UseVisualStyleBackColor = true;

this.btnZero.Click += new System.EventHandler(this.button_Click);

//

// button14

//

this.button14.Location = new System.Drawing.Point(165, 251);

this.button14.Name = "button14";

this.button14.Size = new System.Drawing.Size(54, 23);

this.button14.TabIndex = 14;

this.button14.Text = "button14";

this.button14.UseVisualStyleBackColor = true;

//

// btnDecimal

//

this.btnDecimal.Location = new System.Drawing.Point(24, 251);

this.btnDecimal.Name = "btnDecimal";

this.btnDecimal.Size = new System.Drawing.Size(54, 23);

this.btnDecimal.TabIndex = 13;

this.btnDecimal.Text = ".";

this.btnDecimal.UseVisualStyleBackColor = true;

this.btnDecimal.Click += new System.EventHandler(this.button_Click);

//

// btnAdd

//

this.btnAdd.Location = new System.Drawing.Point(236, 251);

this.btnAdd.Name = "btnAdd";

this.btnAdd.Size = new System.Drawing.Size(54, 23);

this.btnAdd.TabIndex = 20;

this.btnAdd.Text = "+";

this.btnAdd.UseVisualStyleBackColor = true;

this.btnAdd.Click += new System.EventHandler(this.operator_click);

//

// btnSubstract

//

this.btnSubstract.Location = new System.Drawing.Point(236, 209);

this.btnSubstract.Name = "btnSubstract";

this.btnSubstract.Size = new System.Drawing.Size(54, 23);

this.btnSubstract.TabIndex = 21;

this.btnSubstract.Text = "-";

this.btnSubstract.UseVisualStyleBackColor = true;

this.btnSubstract.Click += new System.EventHandler(this.operator_click);

//

// btnFour

//

this.btnFour.Location = new System.Drawing.Point(24, 159);

this.btnFour.Name = "btnFour";

this.btnFour.Size = new System.Drawing.Size(54, 23);

this.btnFour.TabIndex = 22;

this.btnFour.Text = "4";

this.btnFour.UseVisualStyleBackColor = true;

this.btnFour.Click += new System.EventHandler(this.button_Click);

//

// btnFive

//

this.btnFive.Location = new System.Drawing.Point(88, 159);

this.btnFive.Name = "btnFive";

this.btnFive.Size = new System.Drawing.Size(54, 23);

this.btnFive.TabIndex = 23;

this.btnFive.Text = "5";

this.btnFive.UseVisualStyleBackColor = true;

this.btnFive.Click += new System.EventHandler(this.button_Click);

//

// lblOperation

//

this.lblOperation.AutoSize = true;

this.lblOperation.Location = new System.Drawing.Point(33, 46);

this.lblOperation.Name = "lblOperation";

this.lblOperation.Size = new System.Drawing.Size(0, 16);

this.lblOperation.TabIndex = 24;

//

// btnEqual

//

this.btnEqual.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

this.btnEqual.Location = new System.Drawing.Point(313, 209);

this.btnEqual.Name = "btnEqual";

this.btnEqual.Size = new System.Drawing.Size(54, 65);

this.btnEqual.TabIndex = 19;

this.btnEqual.Text = "=";

this.btnEqual.UseVisualStyleBackColor = true;

this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click);

//

// Form1

//

this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(379, 322);

this.Controls.Add(this.lblOperation);

this.Controls.Add(this.btnFive);

this.Controls.Add(this.btnFour);

this.Controls.Add(this.btnSubstract);

this.Controls.Add(this.btnAdd);

this.Controls.Add(this.btnEqual);

this.Controls.Add(this.btnZero);

this.Controls.Add(this.button14);

this.Controls.Add(this.btnDecimal);

this.Controls.Add(this.btnTwo);

this.Controls.Add(this.btnThree);

this.Controls.Add(this.btnOne);

this.Controls.Add(this.btnTimes);

this.Controls.Add(this.btnClear);

this.Controls.Add(this.btn6);

this.Controls.Add(this.btnEight);

this.Controls.Add(this.btnNine);

this.Controls.Add(this.btnDivide);

this.Controls.Add(this.btnClearEntry);

this.Controls.Add(this.button2);

this.Controls.Add(this.btnSeven);

this.Controls.Add(this.txtResult);

this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

this.Margin = new System.Windows.Forms.Padding(4);

this.MaximumSize = new System.Drawing.Size(395, 360);

this.MinimumSize = new System.Drawing.Size(395, 360);

this.Name = "Form1";

this.ShowIcon = false;

this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

this.Text = "Calculator";

this.ResumeLayout(false);

this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox txtResult;

private System.Windows.Forms.Button btnSeven;

private System.Windows.Forms.Button button2;

private System.Windows.Forms.Button btnClearEntry;

private System.Windows.Forms.Button btnDivide;

private System.Windows.Forms.Button btnNine;

private System.Windows.Forms.Button btnEight;

private System.Windows.Forms.Button btnTimes;

private System.Windows.Forms.Button btnClear;

private System.Windows.Forms.Button btn6;

private System.Windows.Forms.Button btnTwo;

private System.Windows.Forms.Button btnThree;

private System.Windows.Forms.Button btnOne;

private System.Windows.Forms.Button btnZero;

private System.Windows.Forms.Button button14;

private System.Windows.Forms.Button btnDecimal;

private System.Windows.Forms.Button btnAdd;

private System.Windows.Forms.Button btnSubstract;

private System.Windows.Forms.Button btnFour;

private System.Windows.Forms.Button btnFive;

private System.Windows.Forms.Label lblOperation;

private System.Windows.Forms.Button btnEqual;

}

}

Exception Class

sing System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Calculator2

{

class DivideException : Exception

{

public DivideException(): base() { }

public DivideException(string message) : base(message)

{

}

}

}

Calculator

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!