This is my first post on this forum so I want to make it useful. I think this
is your assignment so you have to do it on your own. However, I can provide
you some hints to do it on your own.
Creating simple calculator like this is pretty easy. Follow these steps you will
able to complete that program in just a hour:
1) Creating a form, a textbox which is for input and output the result, 10
buttons of number from 0 to 9, and math operations buttons.
2) Creating 2 variables: first one is for store the first inputed number, and
the second one is operation variable which store what operation that
currently use.
Code:
Dim intVal As Integer
Dim intOp As Integer
3) Whenever you click on any operation button, the current value in
textbox will be store in intVal and then store the operation that you clicked
into intOp. intOp = 1 mean the addition operation is used if intOp = 2 mean
the subtraction operation is used, 3 for multipilcation and 4 for division.
The code probally look like this:
Code:
Private Sub bAddtion_Click()
intOp = 1
intVal = Text1.Text
Text1.Text = ""
End Sub
Private Sub bSubtraction_Click()
intOp = 2
intVal = Text1.Text
Text1.Text = ""
End Sub
.... so on.....
4) On the equal button, the program will calculate between those two
values.
Code:
Private Sub bEqual_Click()
Select Case intOp
Case 1:
Text1.Text = intVal + CInt(Text1.Text)
Case 2:
Text1.Text = intVal - CInt(Text1.Text)
Case 3:
Text1.Text = intVal * CInt(Text1.Text)
Case 4:
Text1.Text = intVal \ CInt(Text1.Text)
End Case
intOp = 0
End Sub 5) But some users do not click on the equal button, they directly
click on other operations to continues calculate more number... Here the
solutio. Whenever the users click on each operations button, the program
will check if any operations is currently use. if there is then goto equal
button first.
On each operations button should be look like this
Code:
Private Sub bAddtion_Click()
If intOp <> 0 Then bEqual_Click()
intOp = 1
intVal = Text1.Text
Text1.Text = ""
End Sub
Private Sub bSubtraction_Click()
If intOp <> 0 Then bEqual_Click()
intOp = 2
intVal = Text1.Text
Text1.Text = ""
End Sub
.... so on..... My english is very poor. Hope you are understand what I try to
explain. If anyone think is not the best way to do it or have any other way
please give comment
From siLenTz
My first lovely post!