0% found this document useful (0 votes)
22 views

Lab 1 Manual

This document provides instructions for building a desktop calculator app in C# using Visual Studio. It consists of 4 steps: 1) Setting up the project in Visual Studio, 2) Designing the user interface by adding buttons and a textbox, 3) Implementing the calculator logic by storing operands and operators and performing calculations, and 4) Testing and debugging the app. Example code is provided to append digits to a textbox, store operands and operators, and perform calculations on the "=" button click. The goal is for students to create a functional desktop calculator that can perform basic arithmetic operations.

Uploaded by

millimalaika07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Lab 1 Manual

This document provides instructions for building a desktop calculator app in C# using Visual Studio. It consists of 4 steps: 1) Setting up the project in Visual Studio, 2) Designing the user interface by adding buttons and a textbox, 3) Implementing the calculator logic by storing operands and operators and performing calculations, and 4) Testing and debugging the app. Example code is provided to append digits to a textbox, store operands and operators, and perform calculations on the "=" button click. The goal is for students to create a functional desktop calculator that can perform basic arithmetic operations.

Uploaded by

millimalaika07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab Manual: Building a Desktop Calculator in C#

Objective: The goal of this lab is to guide students through the process of creating a simple desktop
calculator using C# in Visual Studio. By the end of this lab, students should have a functional
calculator that can perform basic arithmetic operations.

Prerequisites:

Basic understanding of C# programming.

Visual Studio installed on your machine.

Lab Steps:

Step 1: Setting up the Project

Open Visual Studio and create a new Windows Forms App (.NET Core) project.

Name the project "CalculatorApp" and choose a suitable location for your project.

Step 2: Designing the User Interface

Open the Form1.cs file.

Drag and drop buttons for digits (0-9), arithmetic operators (+, -, *, /), and other necessary buttons
onto the form.

Add a TextBox to display the input and results.

Step 3: Implementing the Calculator Logic

Double-click on each digit button and implement the code to append the corresponding digit to the
TextBox.

Implement the logic for the arithmetic operator buttons to store the current operator and the first
operand.

Implement the code for the "=" button to perform the calculation based on the stored operator and
operands.

Example Code:

public partial class Form1 : Form

double firstOperand = 0;

string currentOperator = "";

public Form1()

InitializeComponent();

}
private void DigitButton_Click(object sender, EventArgs e)

Button button = (Button)sender;

textBox1.Text += button.Text;

private void OperatorButton_Click(object sender, EventArgs e)

Button button = (Button)sender;

currentOperator = button.Text;

firstOperand = double.Parse(textBox1.Text);

textBox1.Clear();

private void EqualButton_Click(object sender, EventArgs e)

double secondOperand = double.Parse(textBox1.Text);

double result = 0;

switch (currentOperator)

case "+":

result = firstOperand + secondOperand;

break;

case "-":

result = firstOperand - secondOperand;

break;

case "*":

result = firstOperand * secondOperand;

break;
case "/":

if (secondOperand != 0)

result = firstOperand / secondOperand;

else

MessageBox.Show("Cannot divide by zero!");

break;

textBox1.Text = result.ToString();

Step 4: Testing and Debugging

Run the application and test each button to ensure that the calculator performs the expected
operations.

Debug and fix any issues that may arise during testing.

You might also like