1.11M Members

Basic calculator

 
0
 

In this code,while executing it does's show the result.
For eg: Consider addition,in addition result it shows the second value to be added.
How it can be corrected?

Public Class calculator
    Dim add, subt, mul, res As Integer
    Dim div As Double
    Dim ch As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zero.Click
        TextBox1.Text = TextBox1.Text & zero.Text



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = TextBox1.Text & Button2.Text
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        TextBox1.Text = TextBox1.Text & Button3.Text
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox1.Text = TextBox1.Text & Button4.Text
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        TextBox1.Text = TextBox1.Text & Button5.Text
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        TextBox1.Text = Val(Button6.Text)
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        TextBox1.Text = TextBox1.Text & Button7.Text
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        TextBox1.Text = TextBox1.Text & Button8.Text
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        TextBox1.Text = TextBox1.Text & Button9.Text
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        TextBox1.Text = TextBox1.Text & Button10.Text
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Dim add As Integer = 0
        add = Val(TextBox1.Text)
        TextBox1.Clear()
        ch = "plus"
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Dim subt As Integer = 0
        subt = Val(TextBox1.Text)
        TextBox1.Clear()
        ch = "minus"
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Dim mul As Integer = 0
        mul = Val(TextBox1.Text)
        TextBox1.Clear()
        ch = "prod"
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        Dim div As Double = 0
        div = Val(TextBox1.Text)
        TextBox1.Clear()
        ch = "div"
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Select Case ch
            Case "plus"
                Res = add + Val(TextBox1.Text)
                TextBox1.Text = Str(res)

            Case "subt"
                Res = subt - Val(TextBox1.Text)
                TextBox1.Text = Str(Res)
            Case "prod"
                res = mul * Val(TextBox1.Text)
                TextBox1.Text = Str(Res)
            Case "div"
                Res = div / Val(TextBox1.Text)
                TextBox1.Text = Str(Res)

        End Select
    End Sub

    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        TextBox1.Clear()

    End Sub
End Class
 
0
 

First: Try to give meaningfull names to your buttonclick events etc.
Second: You defined teh variable add as global, but you also defined it local to the "plus" click event handler, hence 0(local value) + second input= second input.

 
0
 

Will this build? It seems you are missing your End Sub on your first sub procedure.

 
0
 

One of the things you wat to avoid is duplicating code. One of the problems you end up with is having inconsistent code. For example, you process button 6 differently than the other digit buttons. The method you can use is to code a generic "digit" handler as follows:

Private Sub Digit_Click(sender As System.Object, e As System.EventArgs)
    Dim btn As Button = sender
    txtDisplay.Text &= btn.Text
End Sub

and in your form load you connect the buttons to the handlers as follows

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    For i As Integer = 0 To 9
        AddHandler Me.Controls("btn" & i).Click, AddressOf Digit_Click
    Next

End Sub

This assumes that the ten digit buttons are named btn0, btn1, etc. You could use the same technique to group your handlers for +, -, *, /, etc. then use a Select Case within the handler.

 
0
 

Reverend Jim
I am getting error as "Object reference not set to an instance of an object"
at this line :
AddHandler Me.Controls("btn" & i).Click, AddressOf Digit_Click

 
0
 

You get this error because your button names are different from from the construction in the addhandler statement. Read Reverend Jim's statement carefully

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article