Visual Basic Code Of Simple Calculator
2021年6月20日Download here: http://gg.gg/v2c3v
*Create Calculator In Visual Basic
*Visual Basic Code Of Simple Calculator Free
*Visual Basic Calculator Project
*Simple Calculator In Visual Studio
*Visual Basic Code For Beginners
This is the snippet Simple Calculator on FreeVBCode. The FreeVBCode site provides free Visual Basic code, examples, snippets, and articles on a variety of other topics as well. This is a simplified version of the online calculator. The full version you can get and use here: Calculator. Entering commands from the keyboard. For the work of the simple calculator You can use both ordinary numeric buttons above the keyboard and separate numeric buttons on the right. You can use the Enter key to enter the equal character.
Basic visual 6 basic data types. Application backgroundbasic data types are made by visualbasic 6 is provided to the reader’s data type, and the reader is not required to re define a variable or constant that can be used directly.1 numeric data type2 character type data type3 logical data type4 date type data type5 enumerated data. The following code requires a button, 2 textboxes and a label. The calculation will compute the percentage the the number in text box 2 is of the number in textbox 1. Public Class Form1. Private Sub Button1Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click. Dim Num1 As Double = CDbl(TextBox1.Text).VB.NET GUIBasic Calculator
In the previous sections, we displayed text in console window (black background window). In this section, you will learn to use window forms and other useful components and controls to create GUI applications that increase interactivity.
Step1: Create a project (Windows Forms Application) File->New-> Project..
Step2: Design interface
-Click on Toolbox in the left site to extend it. You will a list of controls as shown in below:
-Drag and drop one textbox and rename it to txtbox(change text in Name field of the properties window to txtbox).
Note: To open Properties window of a control, right-click the control and click Properties.
-Drag and drop 21 command buttons
-Rename button0 to cmd0, button1 to cmd1, button2 to cmd2, button3 to cmd3, button4 to cmd4, button5 to cmd5, button6 to cmd6, button7 to cmd7, button8 to cmd8, button9 to cmd9, button10 to cmdequal, button11 to cmdclear, button 12 to cmdadd, button13 to cmdsubtract, button14 to cmdmultiply, button15 to cmddivide, button16 to cmdsquare, button17 to cmdsqtr, button 18 to cmdcos, button19 to cmd sin, button20 to cmdtan.
-The caption of each button also needs to be changed. For example, you need to change the caption of button0 to 0, button1 to 1, button 2 to 2..etc.
Step3: Write code
To attach code to a control, you need to double-click the control to open code editor. And then you just copy code and paste it.
’Declaring global variables in General section of the form
Dim sign As String
Dim val1 As Double
Dim val2 As Double
Private Sub cmd0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd0.Click
txtbox.Text = txtbox.Text & cmd0.Caption ’get 0
End Sub
Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click
txtbox.Text = txtbox.Text & cmd1.Caption ’get 1
End Sub
Private Sub cmd2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd2.Click
txtbox.Text = txtbox.Text & cmd2.Caption ’get 2
End Sub
Private Sub cmd3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3.Click
txtbox.Text = txtbox.Text & cmd3.Caption ’get 3
End Sub
Private Sub cmd4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd4.Click
txtbox.Text = txtbox.Text & cmd4.Caption ’get 4
End Sub
Private Sub cmd5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd5.Click
txtbox.Text = txtbox.Text & cmd5.Caption ’get 5
End Sub
Private Sub cmd6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handle cmd6.Click
txtbox.Text = txtbox.Text & cmd6.Caption ’get 6
End Sub
Private Sub cmd7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd7.Click
txtbox.Text = txtbox.Text & cmd7.Caption ’get 7
End Sub
Private Sub cmd8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd8.Click
txtbox.Text = txtbox.Text & cmd8.Caption ’get 8
End Sub
Private Sub cmd9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd9.Click
txtbox.Text = txtbox.Text & cmd9.Caption ’get 9
End Sub
Private Sub cmdclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdclear.Click
’clear Texts
txtbox.Text = ’
val1 = 0
val2 = 0
sign = ’
End Sub
Private Sub cmdcos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcos.Click
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Text)
txtbox.Text = Math.Cos(v)
aa: Exit Sub
End Sub
Private Sub cmddivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmddivide.Click
sign = ’/’
On Error GoTo aa
val1 = CDbl(txtbox.Text)
txtbox.Text = ’
aa: Exit Sub
End Sub
Private Sub cmdequal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdequal.Click
On Error GoTo aa
val2 = CDbl(txtbox.Text)
If (sign = ’+’) Then
txtbox.Text = val1 + val2
ElseIf (sign = ’-’) Then
txtbox.Text = val1 - val2
ElseIf (sign = ’*’) Then
txtbox.Text = val1 * val2
Else: txtbox.Text = val1 / val2
End If
aa: Exit Sub
End Sub
Private Sub cmdmultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdmultiply.Click
sign = ’*’
On Error GoTo aa
val1 = CDbl(txtbox.Text)
txtbox.Text = ’
aa: Exit Sub
End Sub
Private Sub cmdplus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdplus.Click
sign = ’+’
On Error GoTo aa
val1 = CDbl(txtbox.Text)
txtbox.Text = ’
aa: Exit Sub
End Sub
Private Sub cmdsin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsin.Click
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Text)
txtbox.Text = Math.Sin(v)
aa: Exit Sub
End Sub
Private Sub cmdsqare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsquare.Click
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Text)
txtbox.Text = v ^ 2
aa: Exit Sub
End Sub
Private Sub cmdsqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsqrt.Click
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Text)
txtbox.Text = Math.Sqr(v)
aa: Exit Sub
End Sub
Private Sub cmdsubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsubtract.Click
sign = ’-’
On Error GoTo aa
val1 = CDbl(txtbox.Text)
txtbox.Text = ’
aa: Exit Sub
End Sub
Private Sub cmdtan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdtan.Click
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Text)
txtbox.Text = Math.Tan(v)
aa: Exit Sub
End Sub
Private Sub txtbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbox.KeyPress
’Allow only number, dot, and backspace characters
If Asc(e.KeyChar) >= Asc(’0’) And Asc(e.KeyChar) <= Asc(’9’) Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46 Then
Exit Sub
Else
e.KeyChar = ’
End If
End Sub
Comments
Akshay
I have one problem,
I want Create on calculator with buttons & i wants to Create an object with class file. How can I do that? What is the code for it?
Please help me..
2015-10-01
gowdhami sree
this pgm is very useful
2015-08-11
david j
A tutorial clearly explained showing beginning OO relationships in Forms
2015-01-19
nansa
thank u soo much.it’s really help me
2014-07-31
Larry
Hello friends.....I have also searched for good tutorial on making calculator.
Best working solution i have found here :-
http://geeksprogrammings.blogspot.com/2013/09/make-calculator-in-net.html
free Download Project is also availiable with good explantion
2014-02-22
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.Note: When creating a program, it is suggested to use a consistent naming convention to easily distinguish controls from one another at a glance.
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: txtInputStep 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.Create Calculator In Visual Basic
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#.Visual Basic Code Of Simple Calculator Free
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# CalculatorVisual Basic Calculator Project
If you want to experiment with the C# code of this basic calculator, you can download this free Visual C# calculator project file below.
Simple Calculator In Visual StudioVisual Basic Code For Beginnersbasic-calculator.rar 327.27 KB
Download here: http://gg.gg/v2c3v
https://diarynote-jp.indered.space
*Create Calculator In Visual Basic
*Visual Basic Code Of Simple Calculator Free
*Visual Basic Calculator Project
*Simple Calculator In Visual Studio
*Visual Basic Code For Beginners
This is the snippet Simple Calculator on FreeVBCode. The FreeVBCode site provides free Visual Basic code, examples, snippets, and articles on a variety of other topics as well. This is a simplified version of the online calculator. The full version you can get and use here: Calculator. Entering commands from the keyboard. For the work of the simple calculator You can use both ordinary numeric buttons above the keyboard and separate numeric buttons on the right. You can use the Enter key to enter the equal character.
Basic visual 6 basic data types. Application backgroundbasic data types are made by visualbasic 6 is provided to the reader’s data type, and the reader is not required to re define a variable or constant that can be used directly.1 numeric data type2 character type data type3 logical data type4 date type data type5 enumerated data. The following code requires a button, 2 textboxes and a label. The calculation will compute the percentage the the number in text box 2 is of the number in textbox 1. Public Class Form1. Private Sub Button1Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click. Dim Num1 As Double = CDbl(TextBox1.Text).VB.NET GUIBasic Calculator
In the previous sections, we displayed text in console window (black background window). In this section, you will learn to use window forms and other useful components and controls to create GUI applications that increase interactivity.
Step1: Create a project (Windows Forms Application) File->New-> Project..
Step2: Design interface
-Click on Toolbox in the left site to extend it. You will a list of controls as shown in below:
-Drag and drop one textbox and rename it to txtbox(change text in Name field of the properties window to txtbox).
Note: To open Properties window of a control, right-click the control and click Properties.
-Drag and drop 21 command buttons
-Rename button0 to cmd0, button1 to cmd1, button2 to cmd2, button3 to cmd3, button4 to cmd4, button5 to cmd5, button6 to cmd6, button7 to cmd7, button8 to cmd8, button9 to cmd9, button10 to cmdequal, button11 to cmdclear, button 12 to cmdadd, button13 to cmdsubtract, button14 to cmdmultiply, button15 to cmddivide, button16 to cmdsquare, button17 to cmdsqtr, button 18 to cmdcos, button19 to cmd sin, button20 to cmdtan.
-The caption of each button also needs to be changed. For example, you need to change the caption of button0 to 0, button1 to 1, button 2 to 2..etc.
Step3: Write code
To attach code to a control, you need to double-click the control to open code editor. And then you just copy code and paste it.
’Declaring global variables in General section of the form
Dim sign As String
Dim val1 As Double
Dim val2 As Double
Private Sub cmd0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd0.Click
txtbox.Text = txtbox.Text & cmd0.Caption ’get 0
End Sub
Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click
txtbox.Text = txtbox.Text & cmd1.Caption ’get 1
End Sub
Private Sub cmd2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd2.Click
txtbox.Text = txtbox.Text & cmd2.Caption ’get 2
End Sub
Private Sub cmd3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3.Click
txtbox.Text = txtbox.Text & cmd3.Caption ’get 3
End Sub
Private Sub cmd4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd4.Click
txtbox.Text = txtbox.Text & cmd4.Caption ’get 4
End Sub
Private Sub cmd5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd5.Click
txtbox.Text = txtbox.Text & cmd5.Caption ’get 5
End Sub
Private Sub cmd6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handle cmd6.Click
txtbox.Text = txtbox.Text & cmd6.Caption ’get 6
End Sub
Private Sub cmd7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd7.Click
txtbox.Text = txtbox.Text & cmd7.Caption ’get 7
End Sub
Private Sub cmd8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd8.Click
txtbox.Text = txtbox.Text & cmd8.Caption ’get 8
End Sub
Private Sub cmd9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd9.Click
txtbox.Text = txtbox.Text & cmd9.Caption ’get 9
End Sub
Private Sub cmdclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdclear.Click
’clear Texts
txtbox.Text = ’
val1 = 0
val2 = 0
sign = ’
End Sub
Private Sub cmdcos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcos.Click
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Text)
txtbox.Text = Math.Cos(v)
aa: Exit Sub
End Sub
Private Sub cmddivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmddivide.Click
sign = ’/’
On Error GoTo aa
val1 = CDbl(txtbox.Text)
txtbox.Text = ’
aa: Exit Sub
End Sub
Private Sub cmdequal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdequal.Click
On Error GoTo aa
val2 = CDbl(txtbox.Text)
If (sign = ’+’) Then
txtbox.Text = val1 + val2
ElseIf (sign = ’-’) Then
txtbox.Text = val1 - val2
ElseIf (sign = ’*’) Then
txtbox.Text = val1 * val2
Else: txtbox.Text = val1 / val2
End If
aa: Exit Sub
End Sub
Private Sub cmdmultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdmultiply.Click
sign = ’*’
On Error GoTo aa
val1 = CDbl(txtbox.Text)
txtbox.Text = ’
aa: Exit Sub
End Sub
Private Sub cmdplus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdplus.Click
sign = ’+’
On Error GoTo aa
val1 = CDbl(txtbox.Text)
txtbox.Text = ’
aa: Exit Sub
End Sub
Private Sub cmdsin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsin.Click
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Text)
txtbox.Text = Math.Sin(v)
aa: Exit Sub
End Sub
Private Sub cmdsqare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsquare.Click
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Text)
txtbox.Text = v ^ 2
aa: Exit Sub
End Sub
Private Sub cmdsqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsqrt.Click
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Text)
txtbox.Text = Math.Sqr(v)
aa: Exit Sub
End Sub
Private Sub cmdsubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsubtract.Click
sign = ’-’
On Error GoTo aa
val1 = CDbl(txtbox.Text)
txtbox.Text = ’
aa: Exit Sub
End Sub
Private Sub cmdtan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdtan.Click
Dim v As Double
On Error GoTo aa
v = CDbl(txtbox.Text)
txtbox.Text = Math.Tan(v)
aa: Exit Sub
End Sub
Private Sub txtbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtbox.KeyPress
’Allow only number, dot, and backspace characters
If Asc(e.KeyChar) >= Asc(’0’) And Asc(e.KeyChar) <= Asc(’9’) Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46 Then
Exit Sub
Else
e.KeyChar = ’
End If
End Sub
Comments
Akshay
I have one problem,
I want Create on calculator with buttons & i wants to Create an object with class file. How can I do that? What is the code for it?
Please help me..
2015-10-01
gowdhami sree
this pgm is very useful
2015-08-11
david j
A tutorial clearly explained showing beginning OO relationships in Forms
2015-01-19
nansa
thank u soo much.it’s really help me
2014-07-31
Larry
Hello friends.....I have also searched for good tutorial on making calculator.
Best working solution i have found here :-
http://geeksprogrammings.blogspot.com/2013/09/make-calculator-in-net.html
free Download Project is also availiable with good explantion
2014-02-22
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.Note: When creating a program, it is suggested to use a consistent naming convention to easily distinguish controls from one another at a glance.
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: txtInputStep 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.Create Calculator In Visual Basic
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#.Visual Basic Code Of Simple Calculator Free
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# CalculatorVisual Basic Calculator Project
If you want to experiment with the C# code of this basic calculator, you can download this free Visual C# calculator project file below.
Simple Calculator In Visual StudioVisual Basic Code For Beginnersbasic-calculator.rar 327.27 KB
Download here: http://gg.gg/v2c3v
https://diarynote-jp.indered.space
コメント