- Visual Studio Simple Calculator Code
- Visual Studio C++ Calculator Tutorial
- How To Use Visual Studio Code
- Visual Studio Calculate Code Coverage
Open src Calculator.sln in Visual Studio to build and run the Calculator app. For a general description of the Calculator project architecture see ApplicationArchitecture.md. To run the UI Tests, you need to make sure that Windows Application Driver (WinAppDriver) is installed. My suspicion is that when creating a calculator, you need to worry about operator precedence. Take for example, 1 + 2. 3. With simply replacing the code the OP had, the total would be 9, rather than the correct answer of 7.
Building a calculator program is one of the best ways to train one’s mind when it comes to creating an algorithm and forming logic. Though a calculator sounds easy to create, it might not that as simple to create as you think even the most basic one that compose only of the MDAS (Multiplication, Division, Addition and Subtraction) operations. Obviously, these operations can be processed by human brain easily but teaching a program to think that way is another thing.
In this tutorial, we are going to create a basic calculator application in Microsoft Visual C# that performs the four basic mathematical operations.
Step 1: Create a New Project
First things first, create a new C# project by going to File > New and choose Windows Form Application template in order for you to create an application with a Windows Forms user interface. Change the project name into “Basic Calculator” to easily find the project later on though you can name it whatever you want. Click OK!
After creating the project, you will now have the basic Windows form in your screen. When you double click the form, it will open the program window and it should already have the basic code such as the using directive, namespace declaration.
Step 2: Declaring Variables
Though variable is not required to perform the functions of a calculator, we are still going to declare for the sake of good practice. Declare the following variables after the public partial class Form1 : Form
Variables Explained:
- operation: Save the operation to be used in the form of “+”, “-“, “*” and “/”
- firstOperand: Save the first number
- secondOperand: Save the second numbe
- answer: Save the calculated result
- clear: Determine what kind of clear function to be used. “S” for single digit and “A” for all digits.
Step 3: Build the Calculator Interface
You can now add the controls necessary to create the calculator interface using the Toolbox located in the left side of the screen. In this case, we are going to use Button and TextBox.
Add the following:
- A TextBox where you can enter numbers and show result
- Buttons that will serve as keypad containing the following:
- Numbers from 0-9
- Decimal point
- Clear function
- Four operations to be used (+. -, *, /)
- Equal button using the equals sign.
To rename a control, go to the property window in the right side and use a naming convention like below (or create your own):
- Button Number 0: btnNum0
- Button Number 1: btnNum1
- Button Number 2: btnNum2
- Button Number 3: btnNum3
- Button Number 4: btnNum4
- Button Number 5: btnNum5
- Button Number 6: btnNum6
- Button Number 7: btnNum7
- Button Number 8: btnNum8
- Button Number 9: btnNum9
- Button Decimal Point: btnDecimal
- Button Equal: btnEqual
- Button Clear Function: btnClear
- Button Addition: btnAdd
- Button Subtraction: btnSubtract
- Button Multiplication: btnMultiply
- Button Division: btnDivide
- Result Textbox: txtInput
Step 4: Program the Number Buttons (0-9) and Decimal Point
Like a typical calculator application, we would like to display the number in the textbox based from whatever button is clicked by the user. To display a value in a textbox control, we will use the text
property of the textbox. The syntax for using the said property is something like this:
There is, however, a problem if you are going to use the syntax above. Every time the the button is clicked, it will always be a single digit and the number that will appear in the textbox is the last clicked button. To fix this problem, use the syntax below instead:
Double click the Button 0 to create a btnNum0_click
event handler and add the above code between the curly braces. After adding the code, it will look like this:
The above code simply gets the initial value of the textbox first, add another digit based from whatever number button is clicked and finally display the result in the textbox. Since the value of the textbox is a String, it will only concatenate the numbers.
Tip: You can also simplify the code by using a “+=” operator. The code will look like this: txtInput.Text += “0”.Repeat the above step for all the numerical input event handlers (number buttons) as well as the decimal button event handler. Change the “0” value from the code and replace it depending which button handler you are in. Your code will now look like this:
Step 5: Program the Clear Function
The clear function will perform two different forms. The first form is to remove a single digit from a group of numbers. This second form is to remove all digits in the textbox which is normally performed after getting the result. The variable clear will determine which among the forms will be performed by the button.
Code Explained:
- Line 3: Since the default value of clear variable is “S”, it will only remove a single digit form the right every time the button is clicked.
- Line 7: This will be activated only when the value of the variable is changed into “A” which will be coded in the equal button (after getting the answer).
Step 6: Program the Mathematical Operations
For the buttons designated to the mathematical operations, a series of actions will be needed to perform to make sure that the calculator will work as expected. This is where the importance of algorithm (step-by step procedure) will be seen. The said actions are as follows:
- Save the value of the textbox into a variable.
- Clear the textbox to give way to the next operand.
- Save the operation to be used.
Double click the Button + to create a btnAdd_click
event handler and add the code like below:
Code Explained:
- Line 2: Save the value of txtInput textbox into firstOperand variable
- Line 3: Place an empty value in the textbox to give way to the next operand
- Line 4: Save the operation into operation variable to determine what operation to be used
Repeat the above step for all the mathematical operations event handlers (+, -, *, and / buttons). Change the “+” value and replace it depending which operation button you are in.
Your code will now look like this:
Step 7: Program the Equal Button
Just like the buttons intended for mathematical operations, the equal button requires a series of action as well. The said actions are as follows:
- Save the value of the textbox into a variable.
- Convert the two operands into integer.
- Determine which operation will be used and perform it.
- Convert the answer to string and display it in the textbox.
Double click the Button = to create a btnEqual_click
event handler and add the code like below:
Code Explained:
- Line 2: Save the value of txtInput textbox into secondOperand variable
- Line 3: Place an empty value in the textbox to give way to the next operand.
- Lines 4 and 5: Convert the value of the two operands from String to Double so that it can be used in mathematical operations.
- Lines 7, 13, 19 and 25: Determine which operation to be used based from the value of the variable operation.
- Lines 8, 14, 20 and 26: Perform the operations to the two operands.
- Lines 9, 15, 21 and 27: Convert the answer to String so that it can be displayed in the textbox.
- Lines 10, 16, 22 and 28: Display the answer into the textbox.
- Line 30: Activate the clear function for all the digits.
Conclusion
After successfully creating the C# calculator program, it is expected that you have gained a basic understanding of how algorithm works as well as how to create a simple program in Visual C#.
As a challenge, you can add more buttons and mathematical formulas and convert this basic program into scientific calculator. You can also add design to make the calculator more appealing.
Download Basic Microsoft Visual C# Calculator
If you want to experiment with the C# code of this basic calculator, you can download this free Visual C# calculator project file below.
There are some steps to build this setup file in visual studio 2012 which are given below:-
Step 1:-First Open Your visual studio-->File -->New -->project -->Select Windows Forms Application -->OK-->Open Solution Explorer (by press View Button) -->Add a New Web Form.
Step 2:- Now Drag and Drop Button ,TextBox and Label control from Toolbox on the Form as shown below:-
Step 4:- Now Right click on each Button -->Go Properties --> Press Events Icon -->Now Create button event handler [Numeric Button(0,1,2,3,4,5,6,7,8,.) ,Operator button(-,+,*,/) ,CA Button ,Ac Button ,Equal button,Off Button] manually as shown below:-
Note:- You have to put all Numeric button to under Numeric_button_click event, similarly put other button also.
Step 3:- Now Write the C# Codes in Form.cs File inside each event handlers which i have created manually as given below:-
Description:-
- Here I have putted all numeric button such as 0,1,2,3,4,5,6,7,8, . in Numeric_button_click Event Handler section.So You have to go each numeric Button's properties -->Events --> Set Click =Numeric_button_Click.
- Here i have putted Operator button such as +,-,*,/ in operator_button_Click EventHandler-->So go each operator Button's properties -->Events --> Set Click =operator_button_Click.
- Similarly others also putted in respective event handlers,You can see it in above c# coding.
Now i am going to create a setup file of above application in visual studio 2010 and 2012 both.But i will show you 'How to create setup file in visual studio 2012'only.Because i have already created setup file in visual studio 2010 so you have to follow same setup fromhere.
There are some steps to create setup file in visual studio 2012 as given below:-
Step 1 :- First open this project in your visual studio-->Open Solution Explorer -->Right click on project (your Application) --> Press Publish as shown below:-
Step 2 :- Now Create a New Folder (calculator) on Your Desktop --> Click Browse Button --> select your setup file path on Your Desktop's Folder (Calculator) -->Proceed With Next Button as shown below:-
Step 3 :- Now Select From CD-ROM or DVD-ROM 's Radio Button -->and Proceed With Next Button as shown below:-
Step 4 :- Now you can select corresponding Radio Button if you want to receive update alert or not of your setup as shown below -->Press Finish Button -->Done.
Step 5 :- Now Open your Desktop Folder (calculator) -->You will see three files --> Install Setup.exe file as shown below:-
Visual Studio Simple Calculator Code
Visual Studio C++ Calculator Tutorial
Step 6 :- Now click start button --> Open your installed calculator application( or open it by Desktop icon) as shown below:-
In this Post I have create two different setup file for this Calculator application.You can download it from the below link.
- First By Visual studio 2010
- Second By Visual studio 2012
Attention :-
- If you are using window xp/vista /7 then you will required .Net Framework 4.5.You can download it from here and install it on your system. Otherwise setup automatically download this framework when you will install it.
- If you are using window 8 then you will not required this framework because it is already installed.
- Please share this post with your friends if this is helpful for you.If any problem comment it.