You are on page 1of 2

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 System.Windows.Forms

Public Class Form1


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

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


' Initialize your form here
' For example, populate the CheckedListBox with items:
clbQuantities.Items.Add("Pens")
clbQuantities.Items.Add("Pencils")
clbQuantities.Items.Add("Ruler")
clbQuantities.Items.Add("72 Page Books")
clbQuantities.Items.Add("288 Pages Books")
clbQuantities.Items.Add("Crayons")

' Set initial result display


txtResult.Text = result.ToString()
End Sub

Private Sub buttonAddition_Click(sender As Object, e As EventArgs) Handles buttonAddition.Click


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

Private Sub buttonSubtraction_Click(sender As Object, e As EventArgs) Handles buttonSubtraction.Click


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

Private Sub buttonMultiplication_Click(sender As Object, e As EventArgs) Handles buttonMultiplication.Click


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

Private Sub buttonDivision_Click(sender As Object, e As EventArgs) Handles buttonDivision.Click


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

Private Sub clbQuantities_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles clbQuantities.ItemCheck


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

Select Case e.Index


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 e.NewValue = CheckState.Checked Then


result += itemValue
Else
result -= itemValue
End If

txtResult.Text = result.ToString()
End Sub

Private Sub buttonEquals_Click(sender As Object, e As EventArgs) Handles buttonEquals.Click


' Perform the calculation based on selectedOperation
Dim currentValue As Double = Double.Parse(txtResult.Text)

Select Case selectedOperation


Case "+"
result += currentValue
Case "-"
result -= currentValue
Case "*"
result *= currentValue
Case "/"
If currentValue <> 0 Then
result /= currentValue
Else
MessageBox.Show("Cannot divide by zero!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Select

' Display the final result


txtResult.Text = result.ToString()
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