Hello everybody

I need help to complete my project.

It's a calculator program. Most of the code is complete

IT SHOULD FUNCTION JUST AS THE PROGRAM CALCULATOR FOUND ON WINDOWS VISTA

I just have two problems

1)It should perform a continuous operation between numbers, without pressing the equal button. In other words my program just performs a mathematical operation between two numbers

For instance : If i click 4 * 2 + 3 and if i click equal it gives me 5, but it should give 11

2) other problem i have is that if i click a number and add/substract/multiply/or divide with another number it dissapears

For instance: if i click 4 and if i clcik + the 4 dissapears from the textbox

THANK YOU!!!!!!!

Since it does not let me attach my file I will post my code

PS: If someone does not understand my problems. PLEASE OPEN A CALCULATOR PROGRAM that you have in your computer, and compare it to my program and you will see the 2 differences/ problems i have

``````Public Class frmMain
'Declare the global variables to be used throughout the form
Dim mfirst As Double
Dim msecond As Double
' Declare the global variables for the operators: Add,Sub,Mul and DIV
Dim mbutton As Double

Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
txtDisplay.Text = txtDisplay.Text & "0"
End Sub

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
'Put the value 1 into the txtDisplay text box

If txtDisplay.Text = "0" Then
txtDisplay.Text = "1"
Else
txtDisplay.Text = txtDisplay.Text & "1"
End If

End Sub

Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
'Put the value 2 into the txtDisplay text box
If txtDisplay.Text = "0" Then
txtDisplay.Text = "2"
Else
txtDisplay.Text = txtDisplay.Text & "2"
End If
End Sub

Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
'Put the value 3 into the txtDisplay text box
If txtDisplay.Text = "0" Then
txtDisplay.Text = "3"
Else
txtDisplay.Text = txtDisplay.Text & "3"
End If
End Sub

Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
'Put the value 4 into the txtDisplay text box
If txtDisplay.Text = "0" Then
txtDisplay.Text = "4"
Else
txtDisplay.Text = txtDisplay.Text & "4"
End If
End Sub

Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
'Put the value 5 into the txtDisplay text box
If txtDisplay.Text = "0" Then
txtDisplay.Text = "5"
Else
txtDisplay.Text = txtDisplay.Text & "5"
End If
End Sub

Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click

If txtDisplay.Text = "0" Then
txtDisplay.Text = "6"
Else
txtDisplay.Text = txtDisplay.Text & "6"
End If
End Sub

Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
If txtDisplay.Text = "0" Then
txtDisplay.Text = "7"
Else
txtDisplay.Text = txtDisplay.Text & "7"
End If
End Sub

Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
If txtDisplay.Text = "0" Then
txtDisplay.Text = "8"
Else
txtDisplay.Text = txtDisplay.Text & "8"
End If
End Sub

Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
If txtDisplay.Text = "0" Then
txtDisplay.Text = "9"
Else
txtDisplay.Text = txtDisplay.Text & "9"
End If
End Sub

Private Sub btnDot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDot.Click
txtDisplay.Text = txtDisplay.Text & "."
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
mbutton = 1
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtDisplay.Text)

txtDisplay.Text = ""
End Sub

Private Sub btnSubstract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubstract.Click

mbutton = 2
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtDisplay.Text)

txtDisplay.Text = " "
End Sub

Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click

mbutton = 3
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtDisplay.Text)

txtDisplay.Text = " "
End Sub

Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click

mbutton = 4
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtDisplay.Text)

txtDisplay.Text = " "
End Sub

Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
msecond = Val(txtDisplay.Text)

Select Case mbutton
Case Is = 1
Case Is = 2
Case Is = 3
Case Is = 4
End Select

End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
mbutton=0
txtDisplay.Text = "0"

End Sub

End Class``````

## All 3 Replies

Solving your first issue with mishandling of the calculation.

This is because of how the add subtract multiply divide buttons are coded.

Your program will currently take the 4 when you press and store to mfirst, then overwrite mfirst with the 3 because you only have one primary storage variable. Hence the 5 as result as the program is storing 3 + 2.

To solve this you could use an array to store the added numbers in as this would allow you to have unlimited numbers in a calculation. A third variable would also be another method and the one i shall use for this solution as I had to make a calculator for college last year :)

I have not written any of the following code in VB as i'm on my Mac and got no IDE on here so it may have the odd error. Hopefully not though.

``Dim mRunningTotal As Single``

Insert the IF function into all four of your button's code

``````Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click

'This will create a total of the calculation to stop any overwriting
If mFirst <> "" Then
mRunningTotal = mRunningTotal + mFirst
End If

mbutton = 3
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtDisplay.Text)
txtDisplay.Text = " "
End Sub``````

``````#
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click

msecond = Val(txtDisplay.Text)

Select Case mbutton

Case Is = 1
'If running total variable not used then
If mRunningTotal = "" Then
Else
'Else use running total variable

Case Is = 2
If mRunningTotal = "" Then
Else

Case Is = 3
If mRunningTotal = "" Then
Else

Case Is = 4
If mRunningTotal = "" Then
Else
End Select

End Sub``````

I cannot work out your second issue from the code and I obviously can't look at in an IDE like i stated above :)

Try the suggestions i have made and get back to me on whether they work or not :)

Mike.

Solving your first issue with mishandling of the calculation.

This is because of how the add subtract multiply divide buttons are coded.

Your program will currently take the 4 when you press and store to mfirst, then overwrite mfirst with the 3 because you only have one primary storage variable. Hence the 5 as result as the program is storing 3 + 2.

To solve this you could use an array to store the added numbers in as this would allow you to have unlimited numbers in a calculation. A third variable would also be another method and the one i shall use for this solution as I had to make a calculator for college last year :)

I have not written any of the following code in VB as i'm on my Mac and got no IDE on here so it may have the odd error. Hopefully not though.

``Dim mRunningTotal As Single``

Insert the IF function into all four of your button's code

``````Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click

'This will create a total of the calculation to stop any overwriting
If mFirst <> "" Then
mRunningTotal = mRunningTotal + mFirst
End If

mbutton = 3
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtDisplay.Text)
txtDisplay.Text = " "
End Sub``````

``````#
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click

msecond = Val(txtDisplay.Text)

Select Case mbutton

Case Is = 1
'If running total variable not used then
If mRunningTotal = "" Then
Else
'Else use running total variable

Case Is = 2
If mRunningTotal = "" Then
Else

Case Is = 3
If mRunningTotal = "" Then
Else

Case Is = 4
If mRunningTotal = "" Then
Else
End Select

End Sub``````

I cannot work out your second issue from the code and I obviously can't look at in an IDE like i stated above :)

Try the suggestions i have made and get back to me on whether they work or not :)

Mike.

First of all thank you for your help

I tried everything you suggested.

However, it does not work. When i run the program it crashes and appears a message:

It highlights

``If mfirst <> "" Then``

and says Conversion from string "" to type 'Double' is not valid. and a bunch of suggestions

Thanks anyway

for the 2nd question, remove the txtdisplay.text="" lines and it should be okay but i don't understand your probe cause thats how a calculator works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.