Ive been trying for a while to make a calculator in Microsoft Visual Web Developer 2008 Express but cant figure it out or come close.
I have all the buttons set up and ID as Button1, Button2,etc. Same with the add,minus,divide,multiply buttons. The tricky part is theres 4 labels and when you click the numbers, the numbers have to appear in the first label and stay there. then you click the add,minus,mult,div button and that appears in the second label and stays there untill cleared. then the next buttons clicked appear in the third label and the anser appears in the 4th label when the enter button is clicked.

We will clear lblResult label at the beginning of each new calculation. This is necessary because the “=” button click event will place the result in lblResult and we don’t want it to show during entry of the next calculation parameters.

Additionally, a blank lblOperator will be the signal that we are entering the first number and not the second.

The code should act as follows.

If there is nothing in lblFirstNumber.Text then
clear lblResult.Text
End If
If there is nothing in lblSecondNumber.Text then
append the number of the key just pressed to whatever is already in lblFirstNumber.Text
Else
append the number of the key just pressed to whatever is already in lblSecondNumber.Text
End If

= Button Click Event

how do i write this^ code?

Execute the code only if both lblFirstNumber and lblSecondNumber contain text.

Create variables of type double to hold the first number, the second number, and the result of the calculation.

Instantiate a new calculator object.

Perform the appropriate calculation by calling a function of the calculator class based on the value in lblOperator.Text.

Concatenate the values in lblFirstNumber.Text, lblOperator.Text, and lblSecondNumber.Text with the equal sign and the value returned from the calculator function. Remember to convert the result of the calculation to a string value. Store the string in lblResult.Text.

Clear lblFirstNumber.Text, lblOperator.Text, and lblSecondNumber.Text by setting their values to “”.

Operator Button Click Events

The click events for the + - * and / keys are similar. Set lblOperator.Text equal to symbol for the operation. For example, the click event for the + key would have code that looks like

lblOperator.Text = “+”

Note that this is the value that will be checked when deciding which function of the calculator class to call in the = button click event.


Clear Button Click Event

Set the Text property of lblFirstNumber, lblSecondNumber, lblOperator, and lblResult to “”.

thanks for any help, i really appreciate it

something i done very quick which may or may not help you. Im not the best vb.net programmer but if it helps thats great!

the form has 4 labels, 1 for first number, calculation type, third number and result

i only have 5 buttons active, three buttons for numbers 1 to 3 and buttons for equals and clear

it is very basic and will only allow for one number in each box but will hopefully help and allow you to expand on it.

Public Class test

    Dim firstchoice As double
    Dim secondchoice As double
    Dim sumtype As String
    Dim buttonnumber As Integer
    Dim HasCalcDone As Boolean

Private Sub oneButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles oneButton.Click
        Dim buttonnumber As Integer = 1

        If Label1.Text = String.Empty Then
            Label1.Text = buttonnumber
            firstchoice = buttonnumber
        Else
            Label3.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub

    Private Sub twoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles twoButton.Click
        Dim buttonnumber As Integer = 2

        If Label1.Text = String.Empty Then
            Label1.Text = buttonnumber
            firstchoice = buttonnumber
        Else
            Label3.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub

    Private Sub threeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles threeButton.Click
        Dim buttonnumber As Integer = 3

        If Label1.Text = String.Empty Then
            Label1.Text = buttonnumber
            firstchoice = buttonnumber
        Else
            Label3.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub

    Private Sub plusButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles plusButton.Click
        If Label1.Text = String.Empty Then
            MsgBox("Please select a number for the first part of the calculation")
        Else

            sumtype = "+"
            Label2.Text = sumtype
        End If
    End Sub

    Private Sub minusButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles minusButton.Click
        If Label1.Text = String.Empty Then
            MsgBox("Please select a number for the first part of the calculation")
        Else
            sumtype = "-"
            Label2.Text = sumtype
        End If
    End Sub

    Private Sub dividedButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dividedButton.Click
        If Label1.Text = String.Empty Then
            MsgBox("Please select a number for the first part of the calculation")
        Else
            sumtype = "/"
            Label2.Text = sumtype
        End If
    End Sub

    Private Sub equalButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles equalButton.Click
        Call AddValues()
        HasCalcDone = True

    End Sub

    Sub AddValues()
        Select Case sumtype
            Case "+"
                Label4.Text = firstchoice + secondchoice
            Case "-"
                Label4.Text = firstchoice - secondchoice
            Case "/"
                Label4.Text = firstchoice / secondchoice
        End Select

    End Sub

    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
        sumtype = ""
        firstchoice = 0
        secondchoice = 0
        Label1.Text = ""
        Label2.Text = ""
        Label3.Text = ""

        HasCalcDone = False
    End Sub
End Class

thanks but i keep getting a bunch of errors trying it that way.
in

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim firstchoice As Double
        Dim secondchoice As Double
        Dim buttonnumber As Integer
        Dim HasCalcDone As Boolean

it tells my firstchoice, secpondchoice,buttonnumber,hascalcdone are all unused local variables.


in the

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim buttonnumber As Integer = 1

        If lblFirstNumber.Text = String.Empty Then
            lblFirstNumber.Text = buttonnumber
            firstchoice = buttonnumber
        Else
            lblSecondNumber.Text = buttonnumber
            secondchoice = buttonnumber
        End If

    End Sub

It tells me name 'firstchoice' is not declared
name 'secondchoice' is not delcared

you need to put the variables at the very top of the code page, above all procedures and below 'Public Class' title

by puting them in page_load sub means that they are only available in that sub and nowhere else!

Public Class Form1

Dim firstchoice As Double
Dim secondchoice As Double
Dim buttonnumber As Integer
Dim HasCalcDone As Boolean

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim buttonnumber As Integer = 1

If lblFirstNumber.Text = String.Empty Then
lblFirstNumber.Text = buttonnumber
firstchoice = buttonnumber
Else
lblSecondNumber.Text = buttonnumber
secondchoice = buttonnumber
End If

End Sub

Ok, im still really lost but alteast started because of you guys, thanks a lot.
So far i have:
In the code behind Calculator.vb in the App_Code folder

Public Class Calculator
    Public Function Add(ByVal a As Double, ByVal b As Double) As Double
        Return a + b
    End Function

    Public Function Subtract(ByVal a As Double, ByVal b As Double) As Double
        Return a - b
    End Function

    Public Function Multiply(ByVal a As Double, ByVal b As Double) As Double
        Return a * b
    End Function

    Public Function Divide(ByVal a As Double, ByVal b As Double) As Double
        Return a / b
    End Function

End Class

In the Default.aspx folder i have all the calculator buttons and labels
In the code behind for it, i have

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim FirstChoice As Double
    Dim secondchoice As Double
    Dim buttonnumber As Integer
    Dim HasCalcDone As Boolean
    Dim myCalculator As New Calculator()

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim buttonnumber As Integer = 1

        If lblFirstNumber.Text = String.Empty Then
            lblFirstNumber.Text = buttonnumber
            FirstChoice = buttonnumber
        Else
            lblSecondNumber.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim buttonnumber As Integer = 2

        If lblFirstNumber.Text = String.Empty Then
            lblFirstNumber.Text = buttonnumber
            FirstChoice = buttonnumber
        Else
            lblSecondNumber.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim buttonnumber As Integer = 3

        If lblFirstNumber.Text = String.Empty Then
            lblFirstNumber.Text = buttonnumber
            FirstChoice = buttonnumber
        Else
            lblSecondNumber.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim buttonnumber As Integer = 4

        If lblFirstNumber.Text = String.Empty Then
            lblFirstNumber.Text = buttonnumber
            FirstChoice = buttonnumber
        Else
            lblSecondNumber.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub

    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim buttonnumber As Integer = 5

        If lblFirstNumber.Text = String.Empty Then
            lblFirstNumber.Text = buttonnumber
            FirstChoice = buttonnumber
        Else
            lblSecondNumber.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim buttonnumber As Integer = 6

        If lblFirstNumber.Text = String.Empty Then
            lblFirstNumber.Text = buttonnumber
            FirstChoice = buttonnumber
        Else
            lblSecondNumber.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub

    Protected Sub Button7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim buttonnumber As Integer = 7

        If lblFirstNumber.Text = String.Empty Then
            lblFirstNumber.Text = buttonnumber
            FirstChoice = buttonnumber
        Else
            lblSecondNumber.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub

    Protected Sub Button8_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button8.Click
        Dim buttonnumber As Integer = 8

        If lblFirstNumber.Text = String.Empty Then
            lblFirstNumber.Text = buttonnumber
            FirstChoice = buttonnumber
        Else
            lblSecondNumber.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub

    Protected Sub Button9_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button9.Click
        Dim buttonnumber As Integer = 9

        If lblFirstNumber.Text = String.Empty Then
            lblFirstNumber.Text = buttonnumber
            FirstChoice = buttonnumber
        Else
            lblSecondNumber.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub

    Protected Sub Button0_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button0.Click
        Dim buttonnumber As Integer = 0

        If lblFirstNumber.Text = String.Empty Then
            lblFirstNumber.Text = buttonnumber
            FirstChoice = buttonnumber
        Else
            lblSecondNumber.Text = buttonnumber
            secondchoice = buttonnumber
        End If
    End Sub


End Class

I tried using the code you gave me ninja, for the add/mult/sub/div buttons but it didnt work. I then tried a bunch to figure it out but kept getting errors. isnt this suppose to be a easy thing to do?

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.