Question Calculator Project Help Please

midnightdav

New member
Joined
Dec 28, 2011
Messages
3
Programming Experience
Beginner
Hello I hope someone could help me. I am very new to programming and have started a tutorial on the web. One of the problems given was to create a simple calculator to use variables. For some reason when I click on the plus and equals buttons I do not get the correct response. The code I have written is:
Public
ClassForm1



Dim value1 AsSingle


Dim value2 AsSingle



PrivateSub Btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn1.Click

txtDisplay.Text = txtDisplay.Text & Btn1.Text


 


EndSub



PrivateSub Btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn2.Click

txtDisplay.Text = txtDisplay.Text & Btn2.Text


EndSub



PrivateSub Btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn3.Click

txtDisplay.Text = txtDisplay.Text & Btn3.Text


EndSub



PrivateSub Btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn4.Click

txtDisplay.Text = txtDisplay.Text & Btn4.Text


EndSub



PrivateSub Btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn5.Click

txtDisplay.Text = txtDisplay.Text & Btn5.Text


EndSub



PrivateSub Btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn6.Click

txtDisplay.Text = txtDisplay.Text & Btn6.Text


EndSub



PrivateSub Btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn7.Click

txtDisplay.Text = txtDisplay.Text & Btn7.Text


EndSub



PrivateSub Btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn8.Click

txtDisplay.Text = txtDisplay.Text & Btn8.Text


EndSub



PrivateSub Btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn9.Click

txtDisplay.Text = txtDisplay.Text & Btn9.Text


EndSub



PrivateSub Btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn0.Click

txtDisplay.Text = txtDisplay.Text & Btn0.Text


EndSub



PrivateSub CmdPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdPlus.Click


Dim Value1 AsInteger


Value1 = Value1 + Val(txtDisplay.Text)

txtDisplay.Clear()


 

 


EndSub



PrivateSub CmdEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEquals.Click



Dim value2 AsInteger


value2 = value1 + Val(txtDisplay.Text)


txtDisplay.Text = value2



EndSub

End
Class


Any pointer would be appreciated as I have not covered debugging yet and finding this a bit confusing as I thought this would work :(
 
It's not great but remove:

Dim Value1 As Integer
Dim Value2 As Integer

from your button clicks and it will work as you expect

these variables can't be seen outside the button click procedures and you've already declared them at class level anyway
 
It's good when it works isn't it :)

One thing to maybe consider in the future is making a sub which does the txtdisplay.text= part, and then parse the data from the button click to the sub, it just means you could make one change, rather than go through every button if you wanted to modify the code, although it wont effect the functionality.
 
Back
Top