School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,168 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,568 people online right now. Registration is fast and FREE... Join Now!




> Create a calculator

sam_benne
Group Icon



post 30 Apr, 2008 - 07:50 AM
Post #1


Here I will shoe you how to create a simple calculator that can add, subtract, divide, multiply and change sign state. Most of the code is commented so it is easy to learn.

For this you will need 19 buttons and one text box (txtNUMBER). Have a look at the picture that is at the bottom on how I have made mine.

first we will declare the variables:
CODE

'Declare the global variables to be used throughout the form
Dim mfirst As Single
Dim msecond As Single
Dim manswer As Single
' Declare the global variables for the operators: Add,Sub,Mul and DIV
Dim mbutton As Integer
'Change the sign of the number from + or - or vice versa
' Depending on its state now they show in txtNUMBER text box
Dim Signstate As Boolean


Second we will make it so that the number buttons actually do something so add the code into the your form:
CODE

Private Sub cmd0_Click()
'Put the value 0 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "0"
End Sub

Private Sub cmd1_Click()
'Put the value 1 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "1"
End Sub

Private Sub cmd2_Click()
'Put the value 2 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "2"
End Sub

Private Sub cmd3_Click()
'Put the value 3 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "3"
End Sub

Private Sub cmd4_Click()
'Put the value 4 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "4"
End Sub

Private Sub cmd5_Click()
'Put the value 5 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "5"
End Sub

Private Sub cmd6_Click()
'Put the value 6 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "6"
End Sub

Private Sub cmd7_Click()
'Put the value 7 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "7"
End Sub

Private Sub cmd8_Click()
'Put the value 8 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "8"
End Sub

Private Sub cmd9_Click()
'Put the value 9 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "9"
End Sub


Noticing that all the number buttons are called cmd# (# is the number) as your buttons need to have these names.

Now we will make the math buttons. The first is the add button:

CODE

Private Sub cmdADD_Click()
'User slected the add button
mbutton = 1
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)

txtNUMBER = ""
End Sub


Subtract:
CODE

Private Sub cmdSUBTRACT_Click()
'User slected the minus button
mbutton = 2
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)

txtNUMBER = ""
End Sub


Multiply:
CODE

Private Sub cmdMULTIPLY_Click()
'User slected the multiply button
mbutton = 3
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)

txtNUMBER = ""
End Sub


Divide:
CODE

Private Sub cmdDIVIDE_Click()
'User slected the Divide button
mbutton = 4
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)

txtNUMBER = ""
End Sub


Now we will make the equals button work otherwise theres no point to all this.
CODE

Private Sub cmdEQUALS_Click()
msecond = Val(txtNUMBER)

Select Case mbutton
Case Is = 1
manswer = mfirst + msecond
Case Is = 2
manswer = mfirst - msecond
Case Is = 3
manswer = mfirst * msecond
Case Is = 4
manswer = mfirst / msecond
End Select
txtNUMBER = manswer
End Sub


Now you can test your calculator to see it work. Once you have test we have just a few more bits to add such as the decimal point:
CODE

Private Sub cmdDOT_Click()
txtNUMBER = txtNUMBER + "."
End Sub


The next bit of code is to change the sign state:
CODE

Private Sub cmdSIGN_Click()
'Sign state = false on load of form
If txtNUMBER = "-" + txtNUMBER Then
    MsgBox "error start again"
End If
If Signstate = False Then
txtNUMBER = "-" + txtNUMBER
Signstate = True
Else
'SignState = True

minusvalue = Val(txtNUMBER)
'Value now positive
minusvalue = Val("-1" * minusvalue)
txtNUMBER = minusvalue
Signstate = False

End If
End Sub


The next two bits are to cancel meaning empty the textbox and to end the program.
CODE

Private Sub cmdEXIT_Click()
Unload frmCALCULATOR
End Sub

Private Sub cmdCANCEL_Click()
'Remove the values in the txtNUMBER text box
txtNUMBER = " "
End Sub


Now your calculator is complete all you have to do is test it. Since this has a lot of code I have added a text file with the code in it. If you do have any problems then just comment me and I will do what I can to help.



Attached File(s)
Attached File  code.txt ( 3.27k ) Number of downloads: 1200
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Posts in this topic
sam_benne   Create a calculator   30 Apr, 2008 - 07:50 AM
Amadeus   I would suggest it may be prudent to implement som...   7 May, 2008 - 09:37 AM
sam_benne   Will update it now so that it does an error check ...   7 May, 2008 - 10:24 AM
sam_benne   Put this: If Val(mfirst) > 0 And Val...   7 May, 2008 - 10:42 AM
tthompson1114   When I am writing the code for each button to add ...   22 Jun, 2008 - 05:32 PM
Reverand Dave   When I am writing the code for each button to add...   14 Aug, 2008 - 08:46 PM
sam_benne   This is Visual Basic 6 not 2008 sorry   22 Jun, 2008 - 06:29 PM
kEsiah   hmm hi der! can i ask u a question? this codes...   26 Jun, 2008 - 12:26 AM
sam_benne   It does have a "0" but yes I can help yo...   26 Jun, 2008 - 06:12 AM
halogod32   If Any One Is Interested I Remade The Whole Code I...   18 Sep, 2008 - 01:46 PM
halogod32   If You Want A Sample Email/IM Me At Gmail: {REMOVE...   18 Sep, 2008 - 01:54 PM
akhileshbc   Hi, why can't you create a control array for t...   27 Sep, 2008 - 09:07 PM
Quin   Is there a way to have it so that when you enter a...   16 Oct, 2008 - 02:00 AM
firebolt94   What about if the equation was 9999999999999999999...   22 Mar, 2009 - 02:02 AM
alfalah88   Here I will shoe you how to create a simple calcu...   29 May, 2009 - 08:19 PM
sam_benne   Have you made sure that you have named everything ...   30 May, 2009 - 01:20 AM
olig1905   thanx for that.. i needed to get this done before ...   9 Aug, 2009 - 09:21 AM
firebolt   Ask in the appropriate forum, in this case, VB.NET...   10 Aug, 2009 - 12:21 AM
Pokebub0825   THX For the help   14 Aug, 2009 - 10:09 PM
kali1613   i have done all and understand all the commands an...   9 Oct, 2009 - 02:15 AM
poly   i have done all and understand all the commands a...   31 Oct, 2009 - 02:07 AM
firebolt   Hi, Could you post that question in the programmi...   11 Oct, 2009 - 03:43 AM


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 06:38PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month