How can I write addition and subtraction operations in VB?

Here is an example code in VB for a simple addition and subtraction operation.

Public Class Form1

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim num1, num2, result As Integer
        num1 = CInt(txtNum1.Text)
        num2 = CInt(txtNum2.Text)

        If rbAdd.Checked Then
            result = num1 + num2
        ElseIf rbSubtract.Checked Then
            result = num1 - num2
        End If

        lblResult.Text = "Result: " & result.ToString()
    End Sub

End Class

Place two text boxes (named txtNum1 and txtNum2), two radio buttons (named rbAdd and rbSubtract), one button (named btnCalculate), and one label (named lblResult) on the form, then simply copy and paste the above code into the code file of the form.

After the user inputs two numbers and chooses either the addition or subtraction sign, when they click the calculate button, the program will compute and display the result in the label.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds