Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I Need Answer in C# SimpleCalculator Code is below: Form-1Desiner.CS namespace SimpleCalculator { partial class Form1 { /// /// Required designer variable. /// private System.ComponentModel.IContainer

I Need Answer in C#

SimpleCalculator Code is below:

Form-1Desiner.CS

namespace SimpleCalculator { 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.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.txtOperand1 = new System.Windows.Forms.TextBox(); this.txtOperator = new System.Windows.Forms.TextBox(); this.txtOperand2 = new System.Windows.Forms.TextBox(); this.txtResult = new System.Windows.Forms.TextBox(); this.btnCalculate = new System.Windows.Forms.Button(); this.btnExit = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label1.Location = new System.Drawing.Point(15, 31); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(97, 20); this.label1.TabIndex = 0; this.label1.Text = "Operand 1 :"; // // label2 // this.label2.AutoSize = true; this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label2.Location = new System.Drawing.Point(27, 90); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(85, 20); this.label2.TabIndex = 1; this.label2.Text = "Operator :"; // // label3 // this.label3.AutoSize = true; this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label3.Location = new System.Drawing.Point(15, 147); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(97, 20); this.label3.TabIndex = 2; this.label3.Text = "Operand 2 :"; // // label4 // this.label4.AutoSize = true; this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label4.Location = new System.Drawing.Point(45, 203); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(67, 20); this.label4.TabIndex = 3; this.label4.Text = "Result :"; // // txtOperand1 // this.txtOperand1.Location = new System.Drawing.Point(137, 29); this.txtOperand1.Name = "txtOperand1"; this.txtOperand1.Size = new System.Drawing.Size(95, 22); this.txtOperand1.TabIndex = 4; // // txtOperator // this.txtOperator.Location = new System.Drawing.Point(137, 88); this.txtOperator.Name = "txtOperator"; this.txtOperator.Size = new System.Drawing.Size(65, 22); this.txtOperator.TabIndex = 5; // // txtOperand2 // this.txtOperand2.Location = new System.Drawing.Point(137, 145); this.txtOperand2.Name = "txtOperand2"; this.txtOperand2.Size = new System.Drawing.Size(95, 22); this.txtOperand2.TabIndex = 6; // // txtResult // this.txtResult.Location = new System.Drawing.Point(137, 201); this.txtResult.Name = "txtResult"; this.txtResult.ReadOnly = true; this.txtResult.Size = new System.Drawing.Size(95, 22); this.txtResult.TabIndex = 7; this.txtResult.TabStop = false; // // btnCalculate // this.btnCalculate.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.btnCalculate.Location = new System.Drawing.Point(19, 274); this.btnCalculate.Name = "btnCalculate"; this.btnCalculate.Size = new System.Drawing.Size(104, 32); this.btnCalculate.TabIndex = 8; this.btnCalculate.Text = "Calculate"; this.btnCalculate.UseVisualStyleBackColor = true; this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click); // // btnExit // this.btnExit.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.btnExit.Location = new System.Drawing.Point(158, 274); this.btnExit.Name = "btnExit"; this.btnExit.Size = new System.Drawing.Size(74, 32); this.btnExit.TabIndex = 9; this.btnExit.Text = "Exit"; this.btnExit.UseVisualStyleBackColor = true; this.btnExit.Click += new System.EventHandler(this.txtExit_Click); // // Form1 // this.AcceptButton = this.btnCalculate; this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(277, 336); this.AcceptButton = btnCalculate; this.CancelButton = btnExit; this.Controls.Add(this.btnExit); this.Controls.Add(this.btnCalculate); this.Controls.Add(this.txtResult); this.Controls.Add(this.txtOperand2); this.Controls.Add(this.txtOperator); this.Controls.Add(this.txtOperand1); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox txtOperand1; private System.Windows.Forms.TextBox txtOperator; private System.Windows.Forms.TextBox txtOperand2; private System.Windows.Forms.TextBox txtResult; private System.Windows.Forms.Button btnCalculate; private System.Windows.Forms.Button btnExit; } }

Form1.CS(Design)

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 SimpleCalculator { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private void btnCalculate_Click(object sender, EventArgs e) { { decimal operand1; decimal operand2; string operatorUsed;

operand1 = Convert.ToDecimal(txtOperand1.Text);

operand2 = Convert.ToDecimal(txtOperand2.Text);

operatorUsed = txtOperator.Text;

decimal result = 0; if (operatorUsed == "+") { result = operand1 + operand2; } else if (operatorUsed == "-") { result = operand1 - operand2; } else if (operatorUsed == "/") { result = operand1 / operand2; } else if (operatorUsed == "*") { result = operand1 * operand2; }

result = Math.Round(result, 4);

txtResult.Text = Convert.ToString(result);

} }

private void txtExit_Click(object sender, EventArgs e) { this.Close(); } } }

image text in transcribed

image text in transcribed

Lab 06-1 Add exception handling to the simple calculator In this exercise, you'll add exception handling to the Simple Calculator form of extra exercise Lab 05-1 Simple Calculator Operand 1 Operator Operand 2: d Result 256 Entry Error Calculate Ext Divide-by-zero error. Please enter a non-zero value for operand 2. OK 1. Open the SimpleCaleulatox project 2. Add a try-catch statement in the btnCalculate Click event handler that will catch any exceptions that occur when the statements in that event handler are executed. If an exception occurs, display a dialog box with the error message, the type of error, and a stack trace. Test the application by entering a nonnumeric value for one of the operands Add three additional catch blocks to the try-catch statement that will catch a 3. an and a ese catc blocks should display a dialog box with an appropriate error message Test the application again by entering a nonnumeric value for one of the operands. Then, enter 0 for the second operand as shown above to see what happens 4

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

6. What data will she need?

Answered: 1 week ago