954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to create a basic addition calculator

Hey,
I have to build an addition calculator. in the application interface should have a label on top which displays the number you clicked then under the label a running total label and then under than there should be a keypad with numbers from 0-9 and a plus sign. my plus sign should react as an equal button. so if i click 3 then + and then click 4...to see 7 i must click the plus button again. so to add a number o the sum the addition button must be clicked

heres what i have so far but i think im way off...i got frustrated because i tried different possibilities but i dont think i have the right idea.. id really appreciate it if someone can help me out

Option Explicit
Dim intTotal

Private Sub Form_Load()
intTotal = 0
lblTotal.Caption = intTotal
End Sub

Private Sub lbl1_Click()
lblClicked = 1
End Sub

Private Sub lbl2_Click()
lblClicked = 2
End Sub

Private Sub lbl3_Click()
lblClicked = 3
End Sub


Private Sub lblPlus_Click()
intTotal = lblClicked + lblClicked
End Sub

kiki256
Newbie Poster
4 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

the way calculators normally work is that
You type the first number
You hit +
You type the second number
You hit = (or plus again)

So you need some variables to remember
1. the first number that was typed,
2. the operator that was clicked

Your numbers should be buttons not labels with code like

private sub cmd1_click()
  lblTotal.text = lblTotal.text & "1"
end sub


when the user click on the plus you have to remember this value and the function

private sub cmdPlus_Click
   FirstNumber = lblTotal.text  ' FirstNumber will need to be a module based variable
   Operator = "+"
   lblTotal.Text = ""
end if


the code for = will need to be something like

private sub cmdEqual_Click()
   select Case Operator
       case "+"
          lblTotal.text = lblTotal.text + FirstNumber
       case "-"
          lblTotal.text = FirstNumber - lblTotal.text
   end select
end if
ChrisPadgham
Posting Pro in Training
413 posts since Sep 2009
Reputation Points: 102
Solved Threads: 78
 

Try also to use CODE tags. Thanks

abelingaw
Posting Whiz in Training
205 posts since Jun 2008
Reputation Points: 66
Solved Threads: 26
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: