27568 total geeks with 3537 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
nalvanzjuli
<ul><li><str ong><a href="http:/ /www.pandora charmscanada online.org/" >pandora outlet online</a></ strong> </li ><li><strong ><a href="http:/ /www.pandora charmscanada online.org/" >pandora silver</a></ strong> </li ><li><strong ><a href="http:/ /www.pandora charmscanad
nalvanzjuli
[b]<a href="http:/ /www.michael korsshoes.or g/.you">will not regret!</a>[ /b] [b][url= http://www.m ic<strong><a href="http:/ /www.michael korsshoes.or g/.you">will not regret!</a>< /strong> <st rong><a href="http:/ /www.michael korsshoes.or g/">michael Kors Online Sa
nalvanzjuli
<strong><a href="http:/ /www.michael korsshoes.or g/.you">will not regret!</a>< /strong> <st rong><a href="http:/ /www.michael korsshoes.or g/">michael Kors Online Sale</a></st rong> <stron g><a href="http:/ /www.michael korsshoes.or g/">Michael Kors Online Store</a><
nalvanzjuli
<title>Ugg Boots Lattice Cardy 3066 Charcoal Women Outlet - $129.00 : Ugg Boots Outlet Online, ugg.brazilfi faworldcup20 14.net</titl e> <meta http-equiv=" Content-Type " content="tex t/html; charset=UTF- 8" /> <meta name="keywor ds" content="Ugg Boots Lattice
nalvanzjuli
[b][url=http ://www.facep eruano.com/] new balance<stro ng><a href="http:/ /www.faceper uano.com/">n ew balance australia</a ></strong> < br> <strong><a href="http:/ /www.faceper uano.com/">n ew balance womens shoes</a></s trong> <br>

Donate
Donate and help us fund new challenges
Donate!
Due Date: Jul 31
July Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
SPUD ? The IETF
protocol Snowden
loves but will
never be used
Don"t want
pranksters
"bricking" your
Android? Just stop
using the internet,
duh ? Go
Rackspace cooking
up
security-secret-sha
ring cloud cabal
Octogenarian
accused of
performing sex act
with a SHRUBBERY
Boffins get the
inside dope, craft
white laser
Bloke cuffed for
blasting camera
drone out of the
skies with shotgun
Critical BIND bug
scores PATCH
YESTERDAY grading
China announces
petascale super for
FAST radiotelescope
Cisco IOS-XE update
time: Squash that
DoS bug
Tired tablets don"t
tickle the
imagination, so
sales fall again
Slashdot
San Francisco"s
Public Works Agency
Tests Paint That
Repels Urine
How Developers Can
Fight Creeping
Mediocrity
Researchers
Demonstrate the
World"s First White
Lasers
Research:
Industrial Networks
Are Vulnerable To
Devastating
Cyberattacks
Beyond Safety: Is
Robotic Surgery
Sustainable?
Obama"s New
Executive Order
Says the US Must
Build an Exascale
Supercomputer
Nokia Announces OZO
360-Degree Camera
For Filming Virtual
Reality
Genetically
Modified Rice Makes
More Food, Less
Greenhouse Gas
Replacing Silicon
With Gallium
Nitride In Chips
Could Reduce Energy
Use By 20%
Microsoft Edge On
Windows 10: the
Browser That Will
Finally Kill IE
Article viewer

A Simple Calculator in Visual Basic for Beginners



Written by:sowoolumide
Published by:SAJChurchey
Published on:2010-03-22 22:37:02
Topic:Visual Basic
Search OSI about Visual Basic.More articles by sowoolumide.
 viewed 44034 times send this article printer friendly

Digg this!
    Rate this article :
This is a more optimized Visual Basic calculator. It contains neater code and lets you understand better on how you can code the same calculator. In short, neater code.

Requirements:

Visual Studio/ Visual basic express.
Understanding of the visual studio or visual basic environment.

1.Launch Visual Studio/ Visual Basic
2. Create a new project and name it whatever you want, e.g Calculator.
............................................................................................



Leave the form in the default name, which should be "form1" or rename it if you want in the properties pane.

Now drag buttons on the form to make it look like the diagram below.
The buttons can be resized to look how you want them to look. The text box was also resized.
For the text on the buttons, click the button you want to change its text value and go to the properties pane, you would see the text property there. you can change it there.



Now use the following names for the buttons on the forms.

BUTTONS
"1" - Button1
"2" - Button2
"3" - Button3
"4" - Button4
"5" - Button5
"6" - Button6
"7" - Button7
*"8" - Button8
*"9" - Button9
*"0" - Button10
*"+" - Button_Add
"" - Button_Multiply
*"-" - Button_Minus
*"=" - Button_Equal
*"C" - Button_Clear
*"/" - Button_Divide
*"." - Button_Point
Text box - Textbox1.text



Double click on the form and add the following code.

Public Class Form1
    ' We create this variables as globals so they can be accessed from any where in the program.

    ' this varible is for the mathematical symbol clicked. e.g +, - .
    Dim sign As Char
    ' This variable stores the first value entered by the user.
    Dim first As Double
    ' This variable stores the second value entered by the user.
    Dim second As Double
    ' This variable stores the answer of the calculation
    Dim ans As Double
    ' This variable stores a value 1 or 0 to tell if the calculation is complete or not.
    Dim stat As Integer

    '
    Sub confirm()
        stat = 1
        first = TextBox1.Text
    End Sub
    ' This subroutine checks the mathematical symbol that was clicked using a select case statement.
    ' This is more efficient then using several if statements.
    ' Then its performs the calculation based on the symbol chosen.
    ' It then sets the calculator status to 1 to show the calculation is complete(stat=1).
    ' It then displays your result in the textbox (TextBox1.Text = ans)
    Sub action()

        Select Case sign
            Case "+"
                ans = first + TextBox1.Text
            Case "-"
                ans = first - TextBox1.Text
            Case "*"
                ans = first * TextBox1.Text
            Case "/"
                ans = first / TextBox1.Text

            Case Else

        End Select
        stat = 1
        TextBox1.Text = ans
    End Sub

    Sub addnum(ByRef a As String)

        If TextBox1.Text = 0 Then
            TextBox1.Text = a
        Else
            TextBox1.Text = TextBox1.Text & a
        End If
        If stat = 1 Then
            TextBox1.Text = a
            stat = 0
        End If
    End Sub

    ' using addnum() in the button click event prevents having to write the same code for every button click.
    ' event. The parameter in the subroutine is the number that should be added to the text button on the button click.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        addnum(1)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        addnum(2)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        addnum(3)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        addnum(4)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        addnum(5)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        addnum(6)
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        addnum(7)
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        addnum(8)
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        addnum(9)
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        addnum(0)
    End Sub

    Private Sub Button_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Add.Click
        sign = "+"
        confirm()
    End Sub

    Private Sub Button_Minus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Minus.Click
        sign = "-"
        confirm()
    End Sub

    Private Sub Button_Multiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Multiply.Click
        sign = "*"
     confirm()
    End Sub

    Private Sub Button_Divide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Divide.Click
        sign = "/"
       confirm()
    End Sub

    Private Sub Button1_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1_Clear.Click
        TextBox1.Text = 0
        stat = 0
        first = TextBox1.Text
    End Sub

    Private Sub Button_Equal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Equal.Click
        action()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button_Point_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Point.Click
        addnum(".")
    End Sub
End Class

Did you like this article? There are hundreds more.

Comments:
Anonymous
2010-12-07 06:52:48
how about in if else statement only???
pointblank
2011-04-20 18:59:18
thx for the code. http://www.dudijaya.com
Anonymous
2011-06-30 11:00:38
good
Anonymous
2015-07-31 05:40:25
0731
jx1007
lebron 12 shoes
ugg uk
kobe 9 elite
cheap soccer jerseys
north face outlet store
coach outlet online
new england patriots jerseys
futbol baratas
coach outlet
chelsea soccer jersey
nike air max
tommy hilfiger outlet online
knicks jersey
vibram fivefingers
hermes outlet store
chanel handbags
coach outlet store
lebron james shoes
cartier watches
burberry outlet,burberry outlet online,burberry outlet store,burberry factory outlet,burberry sale,burberry
nhl jerseys wholesale
hermes outlet
new york giants jerseys
coach outlet online
kevin durant jersey
miami dolphins jerseys
vans for sale
cheap oakley sunglasses
futbol baratas
hermes belts for men
christian louboutin shoes
bottega veneta outlet
the north face jackets
patrick kerney jersey,bobby wagner jersey,russell okung jersey,max unger jersey,paul richardson jersey,terrelle pryor jersey,richard sherman jersey,byron maxwell jersey,derrick coleman jersey,eric winston seahawks jersey
mont blanc outlet
vikings jerseys
toms shoes
cartier bracelet
north face outlet
blake griffin jersey
rolex watches
football shirts
christian louboutin shoes
prom dresses
baltimore ravens jerseys
cheap oakley sunglasses
the north face outlet store
cheap evening dresses
washington redskins jerseys
oakley sunglasses outlet
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
VB Potpourri: Programming, History And Syntax by batterseapower

A nice little collection of the basics of Visual Basic.
Advanced Programming Techniques by batterseapower

A fiendish test covering some of the more obscure elements of Visual Basic 6.


     
Your Ad Here
 
Copyright Open Source Institute, 2006