0% found this document useful (0 votes)
23 views2 pages

Visual Basic Calculator Code Example

This document provides the code for a simple calculator application in Visual Basic using Windows Forms. The calculator allows the user to select quantities from a checklist and perform basic math operations like addition, subtraction, multiplication, and division on the selected quantities. The code initializes the form layout, handles button clicks to select the operation, updates the result when quantities are checked/unchecked, and performs the calculation when the equals button is clicked.

Uploaded by

David Nindi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Visual Basic Calculator Code Example

This document provides the code for a simple calculator application in Visual Basic using Windows Forms. The calculator allows the user to select quantities from a checklist and perform basic math operations like addition, subtraction, multiplication, and division on the selected quantities. The code initializes the form layout, handles button clicks to select the operation, updates the result when quantities are checked/unchecked, and performs the calculation when the equals button is clicked.

Uploaded by

David Nindi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Powered by AI

Copilot

Certainly! Below is the complete code for a simple calculator application in Visual Basic using a Windows Forms
Application. This calculator allows you to select quantities from the clbQuantities CheckedListBox and perform
addition, subtraction, multiplication, or division based on the selected operation.

Imports [Link]

Public Class Form1


Dim result As Double = 0
Dim selectedOperation As String = ""

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles [Link]


' Initialize your form here
' For example, populate the CheckedListBox with items:
[Link]("Pens")
[Link]("Pencils")
[Link]("Ruler")
[Link]("72 Page Books")
[Link]("288 Pages Books")
[Link]("Crayons")

' Set initial result display


[Link] = [Link]()
End Sub

Private Sub buttonAddition_Click(sender As Object, e As EventArgs) Handles [Link]


selectedOperation = "+"
' Store the current result (if needed)
End Sub

Private Sub buttonSubtraction_Click(sender As Object, e As EventArgs) Handles [Link]


selectedOperation = "-"
' Store the current result (if needed)
End Sub

Private Sub buttonMultiplication_Click(sender As Object, e As EventArgs) Handles [Link]


selectedOperation = "*"
' Store the current result (if needed)
End Sub

Private Sub buttonDivision_Click(sender As Object, e As EventArgs) Handles [Link]


selectedOperation = "/"
' Store the current result (if needed)
End Sub

Private Sub clbQuantities_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles [Link]


' Update result based on the checked item
Dim itemValue As Double = 0

Select Case [Link]


Case 0 ' Pens
itemValue = 1.0
Case 1 ' Pencils
itemValue = 0.5
Case 2 ' Ruler
itemValue = 2.0
Case 3 ' 72 Page Books
itemValue = 5.0
Case 4 ' 288 Pages Books
itemValue = 10.0
Case 5 ' Crayons
itemValue = 1.5
' ... handle other items
End Select

If [Link] = [Link] Then


result += itemValue
Else
result -= itemValue
End If

[Link] = [Link]()
End Sub

Private Sub buttonEquals_Click(sender As Object, e As EventArgs) Handles [Link]


' Perform the calculation based on selectedOperation
Dim currentValue As Double = [Link]([Link])

Select Case selectedOperation


Case "+"
result += currentValue
Case "-"
result -= currentValue
Case "*"
result *= currentValue
Case "/"
If currentValue <> 0 Then
result /= currentValue
Else
[Link]("Cannot divide by zero!", "Error", [Link], [Link])
End If
End Select

' Display the final result


[Link] = [Link]()
End Sub
End Class

🧮👍
Feel free to customize the UI, add error handling, and enhance the functionality as needed. Compile and run the
application to test it out!

You might also like