Public Class Form1
Public Class Form1
Public Class Form1
02
05
07
09
10 End Sub
11
13
14 End Sub
15
17
19
20 End Sub
21
23
26
27 End Sub
28
30
32
33 End Sub
34
36
38
39 End Sub
40
42
44
45 End Sub
46
48
49 txtDisplay.Text = txtDisplay.Text & btnfive.Text
50
51 End Sub
52
54
56
57 End Sub
58
61
62 End Sub
63
66
67 End Sub
68
70
73 txtDisplay.Clear()
74
75 End Sub
76
78
80 txtDisplay.Text = total2
End Sub
End Class
view source
print?
1 total1 = btnzero.Text
-Simple Arrays
-Manipulating numbers
-Implementing summing
Let's begin. Firstly we need to create the interface. So you'll need to crank
up ole VB. Put a text box up the top, clear it and give it a decent name like
txtDisplay, now we need to create the numbers, these will be command buttons
and must be an array, the easiest way to do this is to just create one button,
copy and paste it, and when the system asks do you want to create a control
array, you click yes! But what is and array? Well simply put it is a collection
of related variable that share the same type.
We are going to need three variables, one for the first number of type double
(to hold large numbers.) one for the second number, and of course one for the
sign, or type of calculation we are doing. This is how I have declared them.
Dim first As Double
Dim second As Double
Dim sign As String
Not to bad so far, let's get those nasty arrays out of the way.
Private Sub Command1_Click(Index As Integer)
If txtDisplay.Text = "" Then
txtDisplay.Text
= Command1(Index).Caption
Else
txtDisplay.Text
= txtDisplay.Text & Command1(Index).Caption
End If
End Sub
This is the array, what this does is makes the content of the display screen
equal to the caption that has been clicked. In other words if you click the
button with 1 on it the number one will go to the display text box. The same
applies for the following array sub.
On form load set max length of the text box to 10 and dont let anything get
stuck in the text box by any other method then hitting the command buttons.
Now lets see our hard work pay off by adding the calculation sub.
Firstly the sub assigns whats in the display to the variable named second.
It then works out what calculation should be done by finding the current value
for the sign variable, so if the variable is / then it will devide the first
number by the second. And that in essence is the calculator, not to bad I thought,
and easy enough for you to build on and create a much more complex one. There
are a few mor subs we should explore but don't deserve any special attention.
End Sub
End Sub
s = Integer.Parse(TextBox1.Text)
b = "*"
End Sub
/w EPDw UKMTY2
Search
New Member FAQ | Forums | Earn Revenue
Directory | Lobby | Communities | Classifieds | Mentors | Links | New Posts | My
India | Members | Polls |
My Profile
Resources » Articles/Knowledge Sharing » Computer &
Sign In
Technology »
Registe
r
AdSens
e Revenue
How To Create a
Calculator in Visual
Awards &
Gifts Basic.Net
(241)
Rating:
ankit Points: 45 (Rs 35)
(168)
krishna
(104)
Last 7 Days
R Online Members
Pramod ankit
(3015) Tony
babu Thanga perumal "Your's
(2469)
Friend"
vimal
Shathish kumar
(1230)
srinivas raju
jay
more... Chharugh Konwar
Rajeev Pathak
More...
but a not of caution don’t just copy paste the code but
Basic. Net then from the File menu select New Project.
which will change the text in the top bar of the form.
also on the left side you would see the toolbox menu
and click Tool bar and select the standard menu item.
the toolbar at the left side of the screen, and then drag
with your mouse to the location you desire for the Text
Box.
Once you've placed the Text Box you can change the
and "Text" = 0.
4. Select the Button icon on the toolbar and create the
first button just like the way you created the Text Box.
calculator layout.
like this.
calculator input:
String
Double
entered.
part:
bLeft = True
End Sub
7. Now we need to create a subroutine that will handle
If bLeft Then
tbResult.Text = aLeft
Else
tbResult.Text = aRight
End If
End Sub
AddNumber ("0")
End Sub
like this
AddNumber ("1")
End Sub
numbers.
8. To handle the operators plus, minus, times, and
If bLeft Then
aOperator = sNewOperator
bLeft = False
Else
aOperator = sNewOperator
aRight = ""
bLeft = False
End If
End Sub
buttons. Your code for the plus button will look like
this:
AddOperator ("+")
End Sub
like this:
AddOperator ("-")
End Sub
If aLeft <> "" And aRight = "" And aOperator <> ""
Then
aRight = aLeft
End If
If sLeft <> "" And aRight <> "" And aOperator <> ""
Then
iLeft = aLeft
iRight = aRight
Case "+"
Case "-"
Case "/"
Case "*"
End Select
tbResult.Text = iResult
aLeft = iResult
aRight = ""
bLeft = True
End If
End Sub
The rest of the code will run only if left, right, and
copy the result into aLeft, reset aRight, and set bLeft =
True.
iLeft = aLeft
Else
iLeft = 0
End If
iRight = aRight
Else
iRight = 0
End If
If bLeft Then
iLeft = Math.Sqr(iLeft)
tbResult.Text = iLeft
Else
iRight = Math.Sqr(iLeft)
tbResult.Text = iRight
End If
aLeft = iLeft
Else
aLeft = ""
End If
aRight = iRight
Else
aRight = ""
End If
End Sub
if using VB.net.
iRight = aRight
Else
iRight = 0
End If
tbResult.Text = iRight
aRight = iRight
Else
aRight = ""
End If
End If
End Sub
iLeft = aLeft
Else
iLeft = 0
End If
iRight = aRight
Else
iRight = 0
End If
If bLeft Then
iLeft = 1 / iLeft
tbResult.Text = iLeft
Else
iRight = 1 / iRight
tbResult.Text = iRight
End If
aLeft = iLeft
Else
aLeft = ""
End If
aRight = iRight
Else
aRight = ""
End If
End Sub
aLeft = ""
aRight = ""
aOperator = ""
tbResult.Text = "0"
bLeft = True
End Sub
If bLeft Then
aLeft = ""
Else
aRight = ""
End If
tbResult.Text = "0"
End Sub
be a scientific calculator.
A Tester's Blog
Attachments
calculator Code (86486-16434-VB calculator
code.pdf)
Basic.Net . Calculator code in
Post Feedback
Next
More Resources
Resource:
Convert any
Convert any
movie format
movie format to
to 3GP
3GP
Previous
Useful tips on
Resource:
website creation
Checking to earn money
Usability online
Features in a
How to convert
Web
MS Word file to
Application
PDF file
while Testing
Design Pattern
Return to Interview
Resources Questions - Part
3
Post New
Resource Hardware
earn money!
About Us Contact Us Revenue Sharing sites Advertise Privacy Policy Terms Of Use