In the SIMPLEST FORM in Microsoft Visual Studio 2015 & using C# programming how
ID: 3595813 • Letter: I
Question
In the SIMPLEST FORM in Microsoft Visual Studio 2015 & using C# programming how do you code this?: (please include a switch statement like it asks for & keep it as simple as possible please)In this exercise, you'll create a form that accepts two operands and an operator from the user and then performs the requested operation Simple Calculator Operand 1: 8 Operator Operand 2 Resut 77408 1. Start a new project named SimpleCalculator 2. Add labels, text boxes, and buttons to the default form and set the properties of the form and its controls so they appear as shown above. When the user presses the Enter key, the Click event of the Calculate button should fire. When the user presses the Esc key, the Click event of the Exit button should fire. 3. Code a private method named Calculate that uses a Switch statement to perform the requested operation and returns a decimal value. This method should be coded with the following parameters: Parameter decimal Operandl string Operatorl decimal Operand2 Description The value entered for the first operand. One of these four operators: +, -, , or/ The value entered for the second operand. 4. Create an event handler for the Click event of the Calculate button. This event handler should get the two numbers and operand the user enters, call the Calculate method to get the result of the calculation, display the result rounded to four decimal places, move the focus to the Operand 1 text box, and highlight the text there. Create an event handler for the Click event of the Exit button that closes the form. 5. Create an event handler that clears the Result text box if the user changes the text in any of the other text boxes. 6. 7. Test the application to be sure it works correctly
Explanation / Answer
-------------------------------------------------------------------------------------------
Designer code
-------------------------------------------------------------------------------------------
namespace SimpleCalculator
{
partial class Form1
{
/// <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.lblOp1 = new System.Windows.Forms.Label();
this.lblOperator = new System.Windows.Forms.Label();
this.lblOperand2 = new System.Windows.Forms.Label();
this.txtOperand2 = new System.Windows.Forms.TextBox();
this.txtOperator = new System.Windows.Forms.TextBox();
this.txtOperand1 = new System.Windows.Forms.TextBox();
this.btnCalculate = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.lblResult = new System.Windows.Forms.Label();
this.txtResult1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// lblOp1
//
this.lblOp1.AutoSize = true;
this.lblOp1.Location = new System.Drawing.Point(42, 51);
this.lblOp1.Name = "lblOp1";
this.lblOp1.Size = new System.Drawing.Size(57, 13);
this.lblOp1.TabIndex = 0;
this.lblOp1.Text = "Operand 1";
//
// lblOperator
//
this.lblOperator.AutoSize = true;
this.lblOperator.Location = new System.Drawing.Point(42, 77);
this.lblOperator.Name = "lblOperator";
this.lblOperator.Size = new System.Drawing.Size(48, 13);
this.lblOperator.TabIndex = 0;
this.lblOperator.Text = "Operator";
//
// lblOperand2
//
this.lblOperand2.AutoSize = true;
this.lblOperand2.Location = new System.Drawing.Point(42, 104);
this.lblOperand2.Name = "lblOperand2";
this.lblOperand2.Size = new System.Drawing.Size(57, 13);
this.lblOperand2.TabIndex = 0;
this.lblOperand2.Text = "Operand 2";
//
// txtOperand2
//
this.txtOperand2.Location = new System.Drawing.Point(121, 101);
this.txtOperand2.Name = "txtOperand2";
this.txtOperand2.Size = new System.Drawing.Size(100, 20);
this.txtOperand2.TabIndex = 3;
this.txtOperand2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
//
// txtOperator
//
this.txtOperator.Location = new System.Drawing.Point(121, 77);
this.txtOperator.MaxLength = 1;
this.txtOperator.Name = "txtOperator";
this.txtOperator.Size = new System.Drawing.Size(100, 20);
this.txtOperator.TabIndex = 2;
this.txtOperator.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
//
// txtOperand1
//
this.txtOperand1.Location = new System.Drawing.Point(121, 51);
this.txtOperand1.Name = "txtOperand1";
this.txtOperand1.Size = new System.Drawing.Size(100, 20);
this.txtOperand1.TabIndex = 1;
this.txtOperand1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
//
// btnCalculate
//
this.btnCalculate.Location = new System.Drawing.Point(36, 159);
this.btnCalculate.Name = "btnCalculate";
this.btnCalculate.Size = new System.Drawing.Size(75, 23);
this.btnCalculate.TabIndex = 5;
this.btnCalculate.Text = "Calculate";
this.btnCalculate.UseVisualStyleBackColor = true;
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(143, 159);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 23);
this.btnExit.TabIndex = 6;
this.btnExit.Text = "Exit";
this.btnExit.UseVisualStyleBackColor = true;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// lblResult
//
this.lblResult.AutoSize = true;
this.lblResult.Location = new System.Drawing.Point(42, 133);
this.lblResult.Name = "lblResult";
this.lblResult.Size = new System.Drawing.Size(37, 13);
this.lblResult.TabIndex = 0;
this.lblResult.Text = "Result";
//
// txtResult1
//
this.txtResult1.Location = new System.Drawing.Point(119, 127);
this.txtResult1.Name = "txtResult1";
this.txtResult1.Size = new System.Drawing.Size(100, 20);
this.txtResult1.TabIndex = 7;
this.txtResult1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.txtResult1);
this.Controls.Add(this.lblResult);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnCalculate);
this.Controls.Add(this.txtOperand1);
this.Controls.Add(this.txtOperator);
this.Controls.Add(this.txtOperand2);
this.Controls.Add(this.lblOperand2);
this.Controls.Add(this.lblOperator);
this.Controls.Add(this.lblOp1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label lblOp1;
private System.Windows.Forms.Label lblOperator;
private System.Windows.Forms.Label lblOperand2;
private System.Windows.Forms.TextBox txtOperand2;
private System.Windows.Forms.TextBox txtOperator;
private System.Windows.Forms.TextBox txtOperand1;
private System.Windows.Forms.Button btnCalculate;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Label lblResult;
private System.Windows.Forms.TextBox txtResult1;
}
}
--------------------------------------------------------------------------------------------------------------------------------
.Cs 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 SimpleCalculator
{
public partial class Form1 : Form
{
bool IsEnter = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//calculate button event
private void btnCalculate_Click(object sender, EventArgs e)
{
Calculate();
}
/// <summary>
/// Exit button click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
// Calculate method
private void Calculate()
{
try
{
string Operator = txtOperator.Text;//Getting operator
decimal Operand1 = Convert.ToDecimal(txtOperand1.Text);// getting operand 1
decimal Operand2 = Convert.ToDecimal(txtOperand2.Text);// getting operand 2
decimal result = 0;
bool isoperator = true;// Boolean falg for vakidating operator
switch (Operator)//Switch case for operators
{
case "+":
result = Operand1 + Operand2;
break;
case "-":
result = Operand1 - Operand2;
break;
case "*":
result = Operand1 * Operand2;
break;
case "/":
result = Operand1 / Operand2;
break;
default:
isoperator = false;//Validating operator
break;
}
if (isoperator)// If operator is validated
{
txtResult1.Text = result.ToString();
txtOperand1.Focus();
txtOperand1.BackColor = Color.Yellow;//Highlihting operand 1
}
else// If operator is not as expected
{
MessageBox.Show("Error occured. Please check the user input");
}
}
catch (Exception ex)//Exception handling
{
MessageBox.Show("Error occured. Please check the user input");
}
}
/// <summary>
/// Keydown event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txt_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)// checking for escape key press
{
btnExit_Click(null, null);
}
if (e.KeyCode == Keys.Enter)// checking for enter key press
{
IsEnter = true;//enter button falg
Calculate();
}
if (!IsEnter)// If enter button is pressed result fiels will not be cleared
{
txtResult1.Text = string.Empty;
txtOperand1.BackColor = Color.White;
}
IsEnter = false;
}
}
}