Hi everyone. I did a super simple calculator with ASP.net with five buttons: add, subtract, multiply, divide, clear. There are five separate functions for each button's click event. For example:

    Sub addfunction(ByVal s As Object, ByVal e As EventArgs)
        If Not IsNumeric(input1.Text) Or Not IsNumeric(input2.Text) Then
            resultLabel.Text = "You entered an invalid or missing number"
        Else
            number1 = Decimal.Parse(input1.Text)
            number2 = Decimal.Parse(input2.Text)
            addresult = number1 + number2
            resultLabel.Text = addresult.ToString
        End If


            <asp:Button ID="addBtn" runat="server" 
        style="z-index: 1; left: 78px; top: 130px; position: absolute; width: 65px" 
        Text="Add" onclick="addfunction" />

How might I be able to combine all 5 of these functions into one? So for example, all the onclicks will refer to the same function, but will somehow be able to determine which button was clicked? I forget a lot of VB.net from a year ago so any help would be great. Thanks!

You can define one handler to be used for all of the buttons and determine which button was clicked by checking the Text property in the event handler. Can you be more specific as to what you mean by "five separate functions for each button's click event"?

This will show you the number of the number of the Button you clicked.

Private Sub Button1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick, Button2.MouseClick, Button3.MouseClick, Button4.MouseClick, Button5.MouseClick
        Dim strName As String = ""
        Dim text As Button = sender
        strName = text.Name
        Label1.Text = Microsoft.VisualBasic.Right(strName, 1)
        End Sub
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.