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

C# Calculator

This document provides instructions for building a basic calculator application in C#. It outlines steps like adding number and function buttons, storing user input in variables, and using conditional statements to perform different mathematical operations depending on the selected function button. Key aspects covered include using double variables to allow for decimals, parsing string inputs to numbers, clearing the textbox between calculations, and adding an "equals" button that will evaluate expressions using if/else statements.

Uploaded by

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

C# Calculator

This document provides instructions for building a basic calculator application in C#. It outlines steps like adding number and function buttons, storing user input in variables, and using conditional statements to perform different mathematical operations depending on the selected function button. Key aspects covered include using double variables to allow for decimals, parsing string inputs to numbers, clearing the textbox between calculations, and adding an "equals" button that will evaluate expressions using if/else statements.

Uploaded by

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

Building a Calculator in C#

 The number buttons should have their text be


the actual numbers. We will use this text in the
program
 The function buttons names are not important
 Copy and paste the buttons to keep size and
placement consistent.
 Alter the text values to correspond to the
buttons
 Adjust the size of the form and placement of
the elements
 Change the background color of the form
I add the text from the button’s text field to the textbox using
concatenation.

This allows the population of the text box by the button presses.
 We need a variable to hold the first value that
the user inputs
 Let’s use a double for the datatype
 A double has two advantages
◦ It allows for big numbers
◦ It allows for a decimal point
 This is our first math function button
 This will not actually do the calculation
 Calculations will be saved for the equals
button.
 This code does several things:
◦ Allows for more than two numbers to be acted upon
◦ Converts the string to a number (Parse)
◦ Clears the text box for the next input
 Adding a number to itself is a common task
in programming, so a shortcut is commonly
used: +=
 This button will do the work. This will develop
over several stages.
 This adds the two numbers together
 Again, parsing converts strings to numbers
 It resets the first variable to zero for adding
several numbers together
 To include other functions such as subtract,
multiply and divide, how will the calculator
know that we have chosen a different
function?
 We require conditional statements
 A boolean (bool) is a datatype that has only
two states…..true/false
 We need to test the conditional boolean
statements in the equals button and then
supply the code for the correct function
 This requires if/else if statements so that we
can test for all conditions

You might also like