Click here to Skip to main content
15,888,020 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
the problem is that when i do additon it doesnt display result properly,and yes i have only created addition till now...
I have included all the code below i have written so for, for calculator its my home assignment so please kindly help.

Varibles I declared
VB
Public Class Form1
    Dim hldval As Integer
    Dim hldval2 As Integer
    Dim hldsqrt As Integer
    Dim hasdecimal As Boolean = False
    Dim inputstatus As Boolean
    Dim calcfunc As String

For Number 1 button (all other buttons upto 9 have same code)
VB
Private Sub btnone_Click(sender As Object, e As EventArgs) Handles btnone.Click
        If inputstatus Then
            TextBox.Text += btnone.Text
        Else
            TextBox.Text = btnone.Text
            inputstatus = True
        End If

    End Sub

For zero button
VB
Private Sub btnzero_Click(sender As Object, e As EventArgs) Handles btnzero.Click
      If inputstatus Then
          If TextBox.Text.Length >= 1 Then
              TextBox.Text += btnzero.Text
          End If
      End If
  End Sub

On additon button
VB
Private Sub plus_Click(sender As Object, e As EventArgs) Handles plus.Click
     If TextBox.Text <> 0 Then
         If calcfunc = String.Empty Then
             hldval = CType(TextBox.Text, Integer)
             TextBox.Text = ""
         Else
             CalculateTotals()
         End If
         calcfunc = "Add"
         hasdecimal = False
     End If
 End Sub

For equals button
VB
Private Sub result_Click(sender As Object, e As EventArgs) Handles result.Click
       If TextBox.Text <> 0 And hldval <> 0 Then
           CalculateTotals()
           calcfunc = String.Empty
           hasdecimal = False
       End If
   End Sub

For decimal button
VB
Private Sub btndecimal_Click(sender As Object, e As EventArgs) Handles btndecimal.Click
       If Not hasdecimal Then
           If TextBox.Text.Length >= 0 Then
               If Not TextBox.Text = "0" Then
                   TextBox.Text += btndecimal.Text
                   hasdecimal = True
               End If
           ElseIf Not inputstatus Then
               TextBox.Text = "0."
               inputstatus = True
               hasdecimal = True
           End If
       End If
   End Sub

And I have declared the sub to calculate:
VB
Public Sub CalculateTotals()
        hldval = CType(TextBox.Text, Integer)
        hldval2 = CType(TextBox.Text, Integer)
        Select Case calcfunc
            Case "Add"
                hldval = hldval + hldval2
            Case "Subtract"
                hldval = hldval - hldval2
            Case "Divide"
                hldval = hldval / hldval2
            Case "Multiply"
                hldval = hldval * hldval2
            Case "PowerOf"
                hldval = System.Math.Pow(hldval, hldval2)
        End Select
        TextBox.Text = CType(hldval, String)
        inputstatus = False
    End Sub
    End Sub
Posted
Updated 3-Nov-12 0:25am
v2
Comments
OriginalGriff 3-Nov-12 6:22am    
The calculate method you show is the Decimal button click handler repeated!
We will probably need the actual code :laugh:
Use the "Improve question" widget to edit your question and provide better information.
shahharis 3-Nov-12 6:27am    
oh sorry :( i updated my question .. and yes i dont know yet how to post questions in forums i am still begginer so pardon the mistakes
OriginalGriff 3-Nov-12 6:34am    
You did very well! Congratulations on getting the code formatted so it is easy to read - far too many people don't...:sigh:
shahharis 3-Nov-12 6:39am    
thank you... would you please find out where did i do wrong? :( its really urgent thank you in advance

OK.
Now we have the calculate routine, I think I see the problem...
VB
hldval = CType(TextBox.Text, Integer)
hldval2 = CType(TextBox.Text, Integer)
Select Case calcfunc
    Case "Add"
        hldval = hldval + hldval2
Since hldval and hldval2 both get the value from TextBox.Text then will always contain the same value. So you addition will always give you double the last value, subtraction will give you zero, division will give you one, and multiply will square the value.

You need to look at having an internal "result" value - which can be a float or double, (there is no point in converting it to a string and back again) which you use as one of the operands, and into which you put the result so far.

In addition, since you are providing a decimal point, it migh be an idea to use float or double values in your conversion rather than integers! :laugh:
 
Share this answer
 
From:
VB
hldval = CType(TextBox.Text, Integer)
hldval2 = CType(TextBox.Text, Integer)
Select Case calcfunc
    Case "Add"
        hldval = hldval + hldval2

To:
VB
hldval2 = CType(TextBox.Text, Integer)
Select Case calcfunc
    Case "Add"
        hldval = hldval + hldval2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900