0

I'm new at vb.net. i'm making a calculator using only 1 text box and want that my text box show the value( assigned to first variable at run time) along with the operator[+,-,*,/] in upper line and then the value entered in the lower line should be assigned to the second variable and when i press the equal(=) button the required operation should be performed i.e
* add the first variable and the 2nd variable.

I have done a little but it doesn't provide the right answer for example:
* when I enter 3 and click the Add(+) button the cursor goes down in the lower line and
* when i enter 5 and press equal button, text box shows 6 in result instead of 8 and so on. Following is the code

Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
    Operand1 = Val(TextBox1.Text)
    TextBox1.Text = Operand1 & "+" & Environment.NewLine & Operand2
        TextBox1.Focus()
        [Operator] = "+"
End Sub

Private Sub ButtonEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEqual.Click
    Dim Result As Double

    Operand2 = Val(TextBox1.Text)
    Select Case [Operator]
        Case "+"
            Result = Operand1 + Operand2
            TextBox1.Text = Result

        Case "-"
            Result = Operand1 - Operand2
            TextBox1.Text = Result
        Case "*"
            Result = Operand1 * Operand2
            TextBox1.Text = Result
        Case "/"
            Result = Operand1 / Operand2
            TextBox1.Text = Result
    End Select
    TextBox1.Text = Result
End Sub
2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by M_UMAR
1

You're assigning the same value to Operand2 as to Operand1. Try this instead: Operand2=Val(TextBox1.Lines(1))

Votes + Comments
nice
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.