Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The assignment is to create a simple caluclator in C# using Visual Studio. I hav

ID: 3864940 • Letter: T

Question

The assignment is to create a simple caluclator in C# using Visual Studio. I have attached my code below however I keep receiving errors that " the name 'result' does not exist in the current context" for like every variable, method, etc. I would appreciate any help you can give me. Thanks!

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 WindowsCalculator
{
public partial class Form1 : Form
{
decimal value1;
decimal value2;
decimal result;
string operation;
int count;
bool div = false;
decimal input;

private bool TestValue(String numTest)
{
bool test = decimal.TryParse(numTest, out result);
if (test == false)
{
MessageBox.Show("Invalid input");
}
textBox1.Clear();
textBox1.Focus();
return test;
}
public Form1()
{
InitializeComponent();
}

private void add_Click(object sender, EventArgs e) // ADD button
{
div = false;
if (TestValue(textBox1.Text))
{
value1 = input;
count = 2;
}
}

private void subtract_Click(object sender, EventArgs e) // SUBTRACT button
{
div = false;
if (textBox1.Text != "")
{
if (TestValue(textBox1.Text))
{
value1 = input;
}
count = 1;
}
}

private void multiply_Click(object sender, EventArgs e) // MULTIPLY button
{
div = false;
if (TestValue(textBox1.Text))
{
value1 = input;
count = 3;
}
}

private void divide_Click(object sender, EventArgs e) // DIVIDE button
{
div = false;
if (TestValue(textBox1.Text))
{
value1 = input;
count = 4;
}
}

private void equals_Click(object sender, EventArgs e) // EQUALS button
{
div = false;
compute(count);
clear_Click.Enabled = true;
}
}

private void clear_Click(object sender, EventArgs e) // CLEAR button
{
div = false;
TextBox1.Clear();
TextBox1.Focus();
count = 0;
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}
public void compute (int count)
{
switch (count)
{
case 1:
result = value1 + float.Parse(TextBox1.Text);
TextBox1.Text = result.ToString();
break;
case 2:
result = value1 - float.Parse(TextBox1.Text);
TextBox1.Text = result.ToString();
break;
case 3:
result = value1 * float.Parse(TextBox1.Text);
TextBox1.Text = result.ToString();
break;
case 4:
if (float.Parse(TextBox1.Text) == 0)
{
MessageBox.Show("Sorry! This number cannot be divided by 0.");
}
else
{
result = value1 / float.Parse(TextBox1.Text);
}
TextBox1.Text = result.ToString();
break;

default:
break;   
}
}
}

Explanation / Answer

using System;
using System.Windows.Forms;

namespace RedCell.App.Calculator.Example
{
public partial class Form1: Form
{
private double accumulator = 0;
private char lastOperation;

public Form1()
{
InitializeComponent();
}

private void Operator_Pressed(object sender, EventArgs e)
{
// An operator was pressed; perform the last operation and store the new operator.
char operation = (sender as Button).Text[0];
if (operation == 'C')
{
accumulator = 0;
}
else
{
double currentValue = double.Parse(Display.Text);
switch (lastOperation)
{
case '+': accumulator += currentValue; break;
case '-': accumulator -= currentValue; break;
case '×': accumulator *= currentValue; break;
case '÷': accumulator /= currentValue; break;
default: accumulator = currentValue; break;
}
}

lastOperation = operation;
Display.Text = operation == '=' ? accumulator.ToString() : "0";
}

private void Number_Pressed(object sender, EventArgs e)
{
// Add it to the display.
string number = (sender as Button).Text;
Display.Text = Display.Text == "0" ? number : Display.Text + number;
}
}
}

-----------------------------------------------------------

Form1.cs

using System;

using System.Windows.Forms;

namespace RedCell.App.Calculator.Example
{
public partial class Form1: Form
{
private double accumulator = 0;
private char lastOperation;

public Form1()
{
InitializeComponent();
}

private void Operator_Pressed(object sender, EventArgs e)
{
// An operator was pressed; perform the last operation and store the new operator.
char operation = (sender as Button).Text[0];
if (operation == 'C')
{
accumulator = 0;
}
else
{
double currentValue = double.Parse(Display.Text);
switch (lastOperation)
{
case '+': accumulator += currentValue; break;
case '-': accumulator -= currentValue; break;
case '×': accumulator *= currentValue; break;
case '÷': accumulator /= currentValue; break;
default: accumulator = currentValue; break;
}
}

lastOperation = operation;
Display.Text = operation == '=' ? accumulator.ToString() : "0";
}

private void Number_Pressed(object sender, EventArgs e)
{
// Add it to the display.
string number = (sender as Button).Text;
Display.Text = Display.Text == "0" ? number : Display.Text + number;
}
}
}

Form1.Designer.cs

namespace Calculator
{
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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.buttonOne = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.button16 = new System.Windows.Forms.Button();
this.button17 = new System.Windows.Forms.Button();
this.button19 = new System.Windows.Forms.Button();
this.textBox_Result = new System.Windows.Forms.TextBox();
this.labelCurrentOperation = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// buttonOne
//
this.buttonOne.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.buttonOne.Location = new System.Drawing.Point(22, 73);
this.buttonOne.Name = "buttonOne";
this.buttonOne.Size = new System.Drawing.Size(45, 45);
this.buttonOne.TabIndex = 0;
this.buttonOne.Text = "7";
this.buttonOne.UseVisualStyleBackColor = true;
this.buttonOne.Click += new System.EventHandler(this.button_click);
//
// button1
//
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.Location = new System.Drawing.Point(73, 73);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(45, 45);
this.button1.TabIndex = 1;
this.button1.Text = "8";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button_click);
//
// button2
//
this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button2.Location = new System.Drawing.Point(124, 73);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(45, 45);
this.button2.TabIndex = 2;
this.button2.Text = "9";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button_click);
//
// button3
//
this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button3.Location = new System.Drawing.Point(175, 73);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(45, 45);
this.button3.TabIndex = 3;
this.button3.Text = "/";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.operator_click);
//
// button4
//
this.button4.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button4.Location = new System.Drawing.Point(226, 73);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(53, 45);
this.button4.TabIndex = 4;
this.button4.Text = "CE";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button5.Location = new System.Drawing.Point(226, 124);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(53, 45);
this.button5.TabIndex = 9;
this.button5.Text = "C";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button6.Location = new System.Drawing.Point(175, 124);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(45, 45);
this.button6.TabIndex = 8;
this.button6.Text = "*";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.operator_click);
//
// button7
//
this.button7.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button7.Location = new System.Drawing.Point(124, 124);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(45, 45);
this.button7.TabIndex = 7;
this.button7.Text = "6";
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button_click);
//
// button8
//
this.button8.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button8.Location = new System.Drawing.Point(73, 124);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(45, 45);
this.button8.TabIndex = 6;
this.button8.Text = "5";
this.button8.UseVisualStyleBackColor = true;
this.button8.Click += new System.EventHandler(this.button_click);
//
// button9
//
this.button9.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button9.Location = new System.Drawing.Point(22, 124);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(45, 45);
this.button9.TabIndex = 5;
this.button9.Text = "4";
this.button9.UseVisualStyleBackColor = true;
this.button9.Click += new System.EventHandler(this.button_click);
//
// button11
//
this.button11.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button11.Location = new System.Drawing.Point(175, 175);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(45, 45);
this.button11.TabIndex = 13;
this.button11.Text = "-";
this.button11.UseVisualStyleBackColor = true;
this.button11.Click += new System.EventHandler(this.operator_click);
//
// button12
//
this.button12.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button12.Location = new System.Drawing.Point(124, 175);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(45, 45);
this.button12.TabIndex = 12;
this.button12.Text = "3";
this.button12.UseVisualStyleBackColor = true;
this.button12.Click += new System.EventHandler(this.button_click);
//
// button13
//
this.button13.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button13.Location = new System.Drawing.Point(73, 175);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(45, 45);
this.button13.TabIndex = 11;
this.button13.Text = "2";
this.button13.UseVisualStyleBackColor = true;
this.button13.Click += new System.EventHandler(this.button_click);
//
// button14
//
this.button14.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button14.Location = new System.Drawing.Point(22, 175);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(45, 45);
this.button14.TabIndex = 10;
this.button14.Text = "1";
this.button14.UseVisualStyleBackColor = true;
this.button14.Click += new System.EventHandler(this.button_click);
//
// button15
//
this.button15.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button15.Location = new System.Drawing.Point(226, 175);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(53, 96);
this.button15.TabIndex = 19;
this.button15.Text = "=";
this.button15.UseVisualStyleBackColor = true;
this.button15.Click += new System.EventHandler(this.button15_Click);
//
// button16
//
this.button16.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button16.Location = new System.Drawing.Point(175, 226);
this.button16.Name = "button16";
this.button16.Size = new System.Drawing.Size(45, 45);
this.button16.TabIndex = 18;
this.button16.Text = "+";
this.button16.UseVisualStyleBackColor = true;
this.button16.Click += new System.EventHandler(this.operator_click);
//
// button17
//
this.button17.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button17.Location = new System.Drawing.Point(124, 226);
this.button17.Name = "button17";
this.button17.Size = new System.Drawing.Size(45, 45);
this.button17.TabIndex = 17;
this.button17.Text = ".";
this.button17.UseVisualStyleBackColor = true;
this.button17.Click += new System.EventHandler(this.button_click);
//
// button19
//
this.button19.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button19.Location = new System.Drawing.Point(22, 226);
this.button19.Name = "button19";
this.button19.Size = new System.Drawing.Size(96, 45);
this.button19.TabIndex = 15;
this.button19.Text = "0";
this.button19.UseVisualStyleBackColor = true;
this.button19.Click += new System.EventHandler(this.button_click);
//
// textBox_Result
//
this.textBox_Result.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBox_Result.Location = new System.Drawing.Point(22, 40);
this.textBox_Result.Name = "textBox_Result";
this.textBox_Result.Size = new System.Drawing.Size(257, 29);
this.textBox_Result.TabIndex = 20;
this.textBox_Result.Text = "0";
this.textBox_Result.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// labelCurrentOperation
//
this.labelCurrentOperation.AutoSize = true;
this.labelCurrentOperation.BackColor = System.Drawing.SystemColors.Window;
this.labelCurrentOperation.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.labelCurrentOperation.ForeColor = System.Drawing.SystemColors.ControlDark;
this.labelCurrentOperation.Location = new System.Drawing.Point(18, 13);
this.labelCurrentOperation.Name = "labelCurrentOperation";
this.labelCurrentOperation.Size = new System.Drawing.Size(0, 24);
this.labelCurrentOperation.TabIndex = 21;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(305, 291);
this.Controls.Add(this.labelCurrentOperation);
this.Controls.Add(this.textBox_Result);
this.Controls.Add(this.button15);
this.Controls.Add(this.button16);
this.Controls.Add(this.button17);
this.Controls.Add(this.button19);
this.Controls.Add(this.button11);
this.Controls.Add(this.button12);
this.Controls.Add(this.button13);
this.Controls.Add(this.button14);
this.Controls.Add(this.button5);
this.Controls.Add(this.button6);
this.Controls.Add(this.button7);
this.Controls.Add(this.button8);
this.Controls.Add(this.button9);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.buttonOne);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Calculator";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button buttonOne;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.Button button12;
private System.Windows.Forms.Button button13;
private System.Windows.Forms.Button button14;
private System.Windows.Forms.Button button15;
private System.Windows.Forms.Button button16;
private System.Windows.Forms.Button button17;
private System.Windows.Forms.Button button19;
private System.Windows.Forms.TextBox textBox_Result;
private System.Windows.Forms.Label labelCurrentOperation;
}
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Calculator
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote