VB.NET GUI
Basic Calculator
- A simple calculator in Visual Basic The following Visual Basic project contains the source code and Visual Basic examples used for A simple calculator. Though everybody is developing a simple calculator, being a civil engineer i've developed one.
- Visual Basic 2010 Express Tutorial 4 - Simple Calculator Part 1. Simple Calculator Tutorials. Code Download VBT4.zip.
Creating a Calculator Visual Studio C#: This Instrucable will guide you through creating a basic calculator application in C# using Microsoft's Visual Studo development software. Visual Studio is a form of development software made by Microsoft to help developers create programs more eas. In this video we will learn creating a very Simple Calculator using Visual Basics.net. Get the source code from here: If you want something to be made for you from my side. Please don't hesitate in telling me on comments. Code provided in the picture should be simple to understand. Dim is the keyword used to initialize a variable, and new allocates memory. Anything you type in the textbox is of type string by default. Casting is required to use the value as a different type. Enjoy your first creation in VB.Net!
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 Related download for mac.
Comments
Akshay I have one problem, 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. 2014-02-22 |
This is a calculator that resembles a typical scientific calculator , albeit a simpler version. In our version, we have only included the trigonometric functions and the logarithmic functions. The reason of creating a simpler version of the calculator is to help users to learn the programming concepts in a gradual manner and not to confuse them especially those who are learning to program in Visual Basic.
To design the interface, we just to need to modify the interface of the basic calculator that we have created earlier using Visual Basic 6. In this calculator, we have added five more buttons, they are Sin, Cos, Tan, Log and Ln. The common trigonometric functions in Visual Basic 6 are Sin, Cos, Tan and Atn.
a) Sin is the function that computes the value of sine of an angle in radian.
b) Cos is the function that computes the value of cosine of an angle in radian.
c) Tan is the function that computes the value of tangent of an angle in radian.
d) Atn is the function that computes the value of arc tangent of an angle in radian.
Log computes the value of logarithm to base 10 whilst Ln computes the value of natural logarithm.
An angle in degree has to be converted to radian before it can be calculated by the above trigonometric functions. From high school mathematics, we know that π radian is equivalent to 180°; which means 1 radian is equivalent to π divided by 180. Therefore, in order to convert an angle x from degree to radian, we have to multiply x by (π/180). However, there is a small problem because it is rather difficult to obtain the precise value of π, fortunately, there is a way to do it in VB. First of all, we know that an arc tangent of 1 will return the value of 45° which is π/4 radian. So, to obtain the value of π, just multiply the arc tangent of 1 with 4. Now, we are ready to enter the code for all the above additional buttons.
For the Sin button, enter the following code:
Private Sub CmdSin_Click()
sinx = Round(Sin(displayValue * 4 * Atn(1) / 180), 4)
panel.Caption = sinx
End Sub
* The Round function is to correct the output to certain number of decimal places.
For the Cos button, enter the following code:
Private Sub CmdCos_Click()
cosx = Round(Cos(displayValue * 4 * Atn(1) / 180), 4)
panel.Caption = cosx
End Sub
For the Tan button, enter the following code:
Private Sub CmdTan_Click()
tanx = Round(Tan(displayValue * 4 * Atn(1) / 180), 4)
panel.Caption = tanx
End Sub
For the Ln button, enter the following code:
This is a calculator that resembles a typical scientific calculator , albeit a simpler version. In our version, we have only included the trigonometric functions and the logarithmic functions. The reason of creating a simpler version of the calculator is to help users to learn the programming concepts in a gradual manner and not to confuse them especially those who are learning to program in Visual Basic.
To design the interface, we just to need to modify the interface of the basic calculator that we have created earlier using Visual Basic 6. In this calculator, we have added five more buttons, they are Sin, Cos, Tan, Log and Ln. The common trigonometric functions in Visual Basic 6 are Sin, Cos, Tan and Atn.
a) Sin is the function that computes the value of sine of an angle in radian.
b) Cos is the function that computes the value of cosine of an angle in radian.
c) Tan is the function that computes the value of tangent of an angle in radian.
d) Atn is the function that computes the value of arc tangent of an angle in radian.
Log computes the value of logarithm to base 10 whilst Ln computes the value of natural logarithm.
An angle in degree has to be converted to radian before it can be calculated by the above trigonometric functions. From high school mathematics, we know that π radian is equivalent to 180°; which means 1 radian is equivalent to π divided by 180. Therefore, in order to convert an angle x from degree to radian, we have to multiply x by (π/180). However, there is a small problem because it is rather difficult to obtain the precise value of π, fortunately, there is a way to do it in VB. First of all, we know that an arc tangent of 1 will return the value of 45° which is π/4 radian. So, to obtain the value of π, just multiply the arc tangent of 1 with 4. Now, we are ready to enter the code for all the above additional buttons.
For the Sin button, enter the following code:
Private Sub CmdSin_Click()
sinx = Round(Sin(displayValue * 4 * Atn(1) / 180), 4)
panel.Caption = sinx
End Sub
* The Round function is to correct the output to certain number of decimal places.
For the Cos button, enter the following code:
Private Sub CmdCos_Click()
cosx = Round(Cos(displayValue * 4 * Atn(1) / 180), 4)
panel.Caption = cosx
End Sub
For the Tan button, enter the following code:
Private Sub CmdTan_Click()
tanx = Round(Tan(displayValue * 4 * Atn(1) / 180), 4)
panel.Caption = tanx
End Sub
For the Ln button, enter the following code:
Private Sub CmdLn_Click()
panel.Caption = Log(displayValue)
End Sub
and for the Log button, enter the following code:
Private Sub CmdLog_Click()
panel.Caption = Log(displayValue) / Log(10)
End Sub
Visual Basic Code For Simple Calculator Example
The interface is shown below: