Building a Simple Application: Putting It All Together

August 19, 2024

Building a Simple Application: Putting It All Together

In our final lesson, we will build a simple application that incorporates all the concepts learned throughout the course. This hands-on project will reinforce your knowledge and give you practical experience in Visual Basic.

Project Overview

For this project, we will create a basic calculator application that can perform addition, subtraction, multiplication, and division. This application will utilize the concepts of variables, control structures, functions, and user interfaces that we have covered in previous lessons.

Setting Up the Windows Forms Application

1. Open Visual Studio and create a new Windows Forms Application project.

2. Name your project SimpleCalculator.

3. In the Form Designer, add the following controls:

  • Two TextBox controls for user input (name them txtNumber1 and txtNumber2).
  • Four Button controls for each operation (name them btnAdd, btnSubtract, btnMultiply, and btnDivide).
  • A Label control to display the result (name it lblResult).

Writing the Code

Next, we will write the code for each button to perform the respective calculations. Double-click on each button to create an event handler and add the following code:

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    Dim number1 As Double = Convert.ToDouble(txtNumber1.Text)
    Dim number2 As Double = Convert.ToDouble(txtNumber2.Text)
    Dim result As Double = number1 + number2
    lblResult.Text = "Result: " & result.ToString()
End Sub

Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click
    Dim number1 As Double = Convert.ToDouble(txtNumber1.Text)
    Dim number2 As Double = Convert.ToDouble(txtNumber2.Text)
    Dim result As Double = number1 - number2
    lblResult.Text = "Result: " & result.ToString()
End Sub

Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.Click
    Dim number1 As Double = Convert.ToDouble(txtNumber1.Text)
    Dim number2 As Double = Convert.ToDouble(txtNumber2.Text)
    Dim result As Double = number1 * number2
    lblResult.Text = "Result: " & result.ToString()
End Sub

Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click
    Dim number1 As Double = Convert.ToDouble(txtNumber1.Text)
    Dim number2 As Double = Convert.ToDouble(txtNumber2.Text)
    If number2 = 0 Then
        lblResult.Text = "Error: Division by zero"
    Else
        Dim result As Double = number1 / number2
        lblResult.Text = "Result: " & result.ToString()
    End If
End Sub

Testing the Application

Now that we have written the code for our calculator, it’s time to test it. Press F5 to run the application. Enter two numbers in the TextBoxes and click on the respective operation buttons to see the results displayed in the Label control.

Conclusion

Congratulations! You have successfully built a simple calculator application using Visual Basic. This project has allowed you to apply the concepts of variables, control structures, functions, and user interfaces in a practical context. Keep experimenting with your application by adding more features, such as additional operations or input validation.

Thank you for following along with this course on Visual Basic. We hope you have gained valuable skills and knowledge that will aid you in your programming journey!