I could really use some help with this porblem Must be form application in C# pl
ID: 3834770 • Letter: I
Question
I could really use some help with this porblem
Must be form application in C# please make solution very simple as I am a beginner
The code that must be modified
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
string operator1 = txtOperator.Text;
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
decimal result = Calculate(operand1, operator1, operand2);
result = Math.Round(result, 4);
this.txtResult.Text = result.ToString();
txtOperand1.Focus();
}
private decimal Calculate(decimal operand1, string operator1,
decimal operand2)
{
decimal result = 0;
if (operator1 == "+")
result = operand1 + operand2;
else if (operator1 == "-")
result = operand1 - operand2;
else if (operator1 == "*")
result = operand1 * operand2;
else if (operator1 == "/")
result = operand1 / operand2;
return result;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
In this exercise, you’ll add data validation to the Simple Calculator form of extra exercise A5-E1.
1.Open the SimpleCalculator project in the Assignment5SimpleCalculatorValidation directory.
2. Code methods named
IsPresent,
IsDecimal, and
IsWithinRange
that work like the methods described in Murach’s chapter 7 of the book.
3. Code a method named IsOperator that checks that the text box that’s passed to it contains a value of +, -, *, or /.
4. Code a method named IsValidData that checks that the Operand 1 and Operand 2 text boxes contain a decimal value between 0 and 1,000,000 (non-inclusive) and that the Operator text box contains a valid operator.
5. Delete all of the catch blocks from the try-catch statement in the btnCalculate_Click event handler except for the one that catches any exception. Then, add code to this event handler that performs the calculation and displays the result only if the values of the text boxes are valid.
6. Test the application to be sure that all the data is validated properly.
Thanks
I could really use some help with this porblem
Must be form application in C# please make solution very simple as I am a beginner
The code that must be modified
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
string operator1 = txtOperator.Text;
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
decimal result = Calculate(operand1, operator1, operand2);
result = Math.Round(result, 4);
this.txtResult.Text = result.ToString();
txtOperand1.Focus();
}
private decimal Calculate(decimal operand1, string operator1,
decimal operand2)
{
decimal result = 0;
if (operator1 == "+")
result = operand1 + operand2;
else if (operator1 == "-")
result = operand1 - operand2;
else if (operator1 == "*")
result = operand1 * operand2;
else if (operator1 == "/")
result = operand1 / operand2;
return result;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Explanation / Answer
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Ex7_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());
}
}
}
frmCalculator.Designer.cs
namespace Ex7_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.lblOperand1 = new System.Windows.Forms.Label();
this.lblOperator = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.lblResult = new System.Windows.Forms.Label();
this.txtOp1 = new System.Windows.Forms.TextBox();
this.txtOperator = new System.Windows.Forms.TextBox();
this.txtOp2 = 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();
//
// lblOperand1
//
this.lblOperand1.AutoSize = true;
this.lblOperand1.Location = new System.Drawing.Point(12, 45);
this.lblOperand1.Name = "lblOperand1";
this.lblOperand1.Size = new System.Drawing.Size(60, 13);
this.lblOperand1.TabIndex = 0;
this.lblOperand1.Text = "Operand 1:";
//
// lblOperator
//
this.lblOperator.AutoSize = true;
this.lblOperator.Location = new System.Drawing.Point(21, 71);
this.lblOperator.Name = "lblOperator";
this.lblOperator.Size = new System.Drawing.Size(51, 13);
this.lblOperator.TabIndex = 1;
this.lblOperator.Text = "Operator:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 97);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(60, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Operand 2:";
//
// lblResult
//
this.lblResult.AutoSize = true;
this.lblResult.Location = new System.Drawing.Point(12, 9);
this.lblResult.Name = "lblResult";
this.lblResult.Size = new System.Drawing.Size(40, 13);
this.lblResult.TabIndex = 3;
this.lblResult.Text = "Result:";
//
// txtOp1
//
this.txtOp1.Location = new System.Drawing.Point(78, 42);
this.txtOp1.Name = "txtOp1";
this.txtOp1.Size = new System.Drawing.Size(100, 20);
this.txtOp1.TabIndex = 4;
this.txtOp1.TextChanged += new System.EventHandler(this.txtOp1_TextChanged);
//
// txtOperator
//
this.txtOperator.Location = new System.Drawing.Point(78, 68);
this.txtOperator.MaxLength = 1;
this.txtOperator.Name = "txtOperator";
this.txtOperator.Size = new System.Drawing.Size(43, 20);
this.txtOperator.TabIndex = 5;
this.txtOperator.TextChanged += new System.EventHandler(this.txtOperator_TextChanged);
this.txtOperator.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtOperator_KeyDown);
//
// txtOp2
//
this.txtOp2.Location = new System.Drawing.Point(78, 94);
this.txtOp2.Name = "txtOp2";
this.txtOp2.Size = new System.Drawing.Size(100, 20);
this.txtOp2.TabIndex = 6;
this.txtOp2.TextChanged += new System.EventHandler(this.txtOp2_TextChanged);
//
// txtResult
//
this.txtResult.Location = new System.Drawing.Point(58, 6);
this.txtResult.Name = "txtResult";
this.txtResult.ReadOnly = true;
this.txtResult.Size = new System.Drawing.Size(187, 20);
this.txtResult.TabIndex = 7;
//
// btnCalculate
//
this.btnCalculate.Location = new System.Drawing.Point(57, 143);
this.btnCalculate.Name = "btnCalculate";
this.btnCalculate.Size = new System.Drawing.Size(75, 23);
this.btnCalculate.TabIndex = 8;
this.btnCalculate.Text = "&Calculate";
this.btnCalculate.UseVisualStyleBackColor = true;
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
//
// btnExit
//
this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnExit.Location = new System.Drawing.Point(138, 143);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 23);
this.btnExit.TabIndex = 9;
this.btnExit.Text = "E&xit";
this.btnExit.UseVisualStyleBackColor = true;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// frmCalculator
//
this.AcceptButton = this.btnCalculate;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnExit;
this.ClientSize = new System.Drawing.Size(276, 178);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnCalculate);
this.Controls.Add(this.txtResult);
this.Controls.Add(this.txtOp2);
this.Controls.Add(this.txtOperator);
this.Controls.Add(this.txtOp1);
this.Controls.Add(this.lblResult);
this.Controls.Add(this.label3);
this.Controls.Add(this.lblOperator);
this.Controls.Add(this.lblOperand1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frmCalculator";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Simple Calculator";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label lblOperand1;
private System.Windows.Forms.Label lblOperator;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label lblResult;
private System.Windows.Forms.TextBox txtOp1;
private System.Windows.Forms.TextBox txtOperator;
private System.Windows.Forms.TextBox txtOp2;
private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.Button btnCalculate;
private System.Windows.Forms.Button btnExit;
}
}
frmCalculator.cs
using System;
using System.Windows.Forms;
namespace Ex7_SimpleCalculator
{
public partial class frmCalculator : Form
{
public frmCalculator()
{
InitializeComponent();
}
/* Calculate Method */
private decimal calculate(Decimal a, String Operator, Decimal b)
{
Decimal result;
switch (Operator[0])
{
case '+':
result = a + b;
return result;
case '-':
result = a - b;
return result;
case '*':
result = a * b;
return result;
case '/':
result = a / b;
return result;
default:
MessageBox.Show("Check your calculation and try again.", "Calculation formatting error");
return result = 0;
}
}
/* Exit Button */
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
/* Calculate Button */
private void btnCalculate_Click(object sender, EventArgs e)
{
if (IsValidData(txtOp1, txtOp2, txtOperator))
{
try
{
txtResult.TextAlign = HorizontalAlignment.Left;
txtResult.Text = calculate(
Decimal.Parse(txtOp1.Text),
txtOperator.Text,
Decimal.Parse(txtOp2.Text)).ToString("n4");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString());
}
txtOp1.Focus();
}
}
/* Clear Result field upon text change */
private void txtOp1_TextChanged(object sender, EventArgs e)
{
txtResult.Clear();
}
private void txtOperator_TextChanged(object sender, EventArgs e)
{
txtResult.Clear();
}
private void txtOp2_TextChanged(object sender, EventArgs e)
{
txtResult.Clear();
}
private bool IsPresent(TextBox Op1, TextBox Op2, TextBox Operator)
{
if (Op1.Text == "")
{
MessageBox.Show("Value missing in Operator 1!", "Calculation Error");
Op1.Focus();
return false;
}
else if (Op2.Text == "")
{
MessageBox.Show("Value missing in Operator 2!", "Calculation Error");
Op2.Focus();
return false;
}
else if (Operator.Text == "")
{
MessageBox.Show("Value missing in Operator 1!", "Calculation Error");
Operator.Focus();
return false;
}
return true;
}
private bool IsDecimal(TextBox textbox, String name)
{
Decimal number = 0m;
if (Decimal.TryParse(textbox.Text, out number))
{
return true;
}
else
{
MessageBox.Show(name + " must be a decimal value.", "Entry Error");
textbox.Focus();
return false;
}
}
private bool IsWithinRange(TextBox textbox, String name, Decimal min, Decimal max)
{
decimal number = Convert.ToDecimal(textbox.Text);
if (number < min || number > max)
{
MessageBox.Show(name + " must be between " + min.ToString() + " and " + max.ToString() + ".", "Entry Error");
textbox.Focus();
return false;
}
return true;
}
private bool IsOperator(TextBox textbox)
{
String Op = textbox.Text;
switch (Op[0])
{
case '+':
return true;
case '-':
return true;
case '*':
return true;
case '/':
return true;
default:
MessageBox.Show("Check the value of the operator.", "Entry Error");
return false;
}
}
private bool IsValidData(TextBox num1, TextBox num2, TextBox op)
{
String strNum1 = "Operator 1";
String strNum2 = "Operator 2";
Decimal min = 0;
Decimal max = 1000000;
if (IsDecimal(num1, strNum1) &&
IsDecimal(num2, strNum2) &&
IsWithinRange(num1, strNum1, min, max) &&
IsWithinRange(num2, strNum2, min, max) &&
IsOperator(op))
{
return true;
}
else
{
return false;
}
}
private void txtOperator_KeyDown(object sender, KeyEventArgs e)
{
txtOperator.Clear();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.