Simple Calculator in (VB) Visual Basic with Source Code

How to Create a Simple Calculator in VB.NET: Step-by-Step Guide with Code

Visual Basic .NET (VB.NET) is an approachable programming language, especially for beginners, offering ease of use for developing Windows-based applications. One of the most common beginner projects is creating a basic calculator. In this post, we’ll walk through how to create a simple calculator in VB.NET, with step-by-step instructions and coding explanations.

Requirements

  • Visual Studio (any version)
  • Basic knowledge of VB.NET
  • Familiarity with Windows Forms

Step 1: Create a New Project

  1. Open Visual Studio and create a new project.
  2. Select Windows Forms App (.NET Framework).
  3. Name your project, for example, SimpleCalculator.

Step 2: Design the User Interface (UI)

Designing the UI involves adding buttons, labels, and a text box for displaying the result.

  1. Drag and Drop Controls from the Toolbox to your form:
    • TextBox (for displaying input and output)
    • Buttons for numbers (0-9)
    • Buttons for operations (+, -, *, /, =)
    • Clear (C) Button

Example Layout

  • Place the TextBox at the top of the form for displaying results and inputs.
  • Add buttons for the numbers and operations. A simple grid layout works well:
    • Row 1: 7, 8, 9, +
    • Row 2: 4, 5, 6, -
    • Row 3: 1, 2, 3, *
    • Row 4: 0, C, =, /

Step 3: Write the Code

Let’s dive into the code now, starting with variable declarations.

Code Setup

  1. Declare Variables: You'll need two variables to store numbers, and another to store the operator selected by the user.
vb

Public Class Form1 Dim firstNumber As Double Dim secondNumber As Double Dim operation As String Dim result As Double End Class

Step 4: Implement Number Buttons

We want to display the numbers clicked on the calculator in the TextBox.

  1. Double-click on each button (0-9) and write the following code to display the corresponding number in the TextBox:
vb

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click TextBox1.Text = TextBox1.Text & "1" End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click TextBox1.Text = TextBox1.Text & "2" End Sub ' Repeat for buttons 0-9 by changing the number in TextBox1.Text

Step 5: Implement Operator Buttons

When an operator button (+, -, *, /) is clicked, the first number should be stored and the operator saved for use during calculation.

vb

Private Sub Add_Click(sender As Object, e As EventArgs) Handles Add.Click firstNumber = Double.Parse(TextBox1.Text) operation = "+" TextBox1.Clear() End Sub Private Sub Subtract_Click(sender As Object, e As EventArgs) Handles Subtract.Click firstNumber = Double.Parse(TextBox1.Text) operation = "-" TextBox1.Clear() End Sub Private Sub Multiply_Click(sender As Object, e As EventArgs) Handles Multiply.Click firstNumber = Double.Parse(TextBox1.Text) operation = "*" TextBox1.Clear() End Sub Private Sub Divide_Click(sender As Object, e As EventArgs) Handles Divide.Click firstNumber = Double.Parse(TextBox1.Text) operation = "/" TextBox1.Clear() End Sub

Step 6: Implement the Equals Button (=)

When the equals button is clicked, the second number is entered and the result of the operation is displayed in the TextBox.

vb

Private Sub Equals_Click(sender As Object, e As EventArgs) Handles Equals.Click secondNumber = Double.Parse(TextBox1.Text) Select Case operation Case "+" result = firstNumber + secondNumber Case "-" result = firstNumber - secondNumber Case "*" result = firstNumber * secondNumber Case "/" If secondNumber <> 0 Then result = firstNumber / secondNumber Else MessageBox.Show("Cannot divide by zero!") End If End Select TextBox1.Text = result.ToString() End Sub

Step 7: Implement the Clear Button (C)

To clear the TextBox and reset variables, use the following code:

vb

Private Sub Clear_Click(sender As Object, e As EventArgs) Handles Clear.Click TextBox1.Clear() firstNumber = 0 secondNumber = 0 operation = "" End Sub

Step 8: Running the Application

Once all buttons have their respective event handlers, you can run the application by pressing F5. The calculator should now perform basic arithmetic operations such as addition, subtraction, multiplication, and division.

Full Code Example

Here is the complete code for the simple calculator:

vb

Public Class Form1 Dim firstNumber As Double Dim secondNumber As Double Dim operation As String Dim result As Double Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click TextBox1.Text = TextBox1.Text & "1" End Sub Private Sub Add_Click(sender As Object, e As EventArgs) Handles Add.Click firstNumber = Double.Parse(TextBox1.Text) operation = "+" TextBox1.Clear() End Sub Private Sub Equals_Click(sender As Object, e As EventArgs) Handles Equals.Click secondNumber = Double.Parse(TextBox1.Text) Select Case operation Case "+" result = firstNumber + secondNumber Case "-" result = firstNumber - secondNumber Case "*" result = firstNumber * secondNumber Case "/" If secondNumber <> 0 Then result = firstNumber / secondNumber Else MessageBox.Show("Cannot divide by zero!") End If End Select TextBox1.Text = result.ToString() End Sub Private Sub Clear_Click(sender As Object, e As EventArgs) Handles Clear.Click TextBox1.Clear() firstNumber = 0 secondNumber = 0 operation = "" End Sub End Class

Conclusion

This tutorial walks you through creating a simple calculator in VB.NET. This project is a great starting point for learning event handling, variable manipulation, and user interface design. Once comfortable with this, you can further enhance the calculator by adding functionalities like decimal support, advanced mathematical operations, or even building a scientific calculator!

Previous Post Next Post

نموذج الاتصال