0% found this document useful (0 votes)
1 views11 pages

Basic Programming1

This document contains the code for a simple calculator application built using Windows Forms in VB.NET. It includes a graphical user interface with buttons for digits, operations, and a display for the current input. The application handles basic arithmetic operations and updates the display based on user input.
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)
1 views11 pages

Basic Programming1

This document contains the code for a simple calculator application built using Windows Forms in VB.NET. It includes a graphical user interface with buttons for digits, operations, and a display for the current input. The application handles basic arithmetic operations and updates the display based on user input.
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

Imports [Link].

Forms

Imports [Link]

Public Class Form1

Inherits Form

' Display text box

Private WithEvents txtDisplay As TextBox

' Digit buttons

Private WithEvents btn0 As Button

Private WithEvents btn1 As Button

Private WithEvents btn2 As Button

Private WithEvents btn3 As Button

Private WithEvents btn4 As Button

Private WithEvents btn5 As Button

Private WithEvents btn6 As Button

Private WithEvents btn7 As Button

Private WithEvents btn8 As Button

Private WithEvents btn9 As Button

Private WithEvents btn0 As Button

Private WithEvents btn1 As Button

Private WithEvents btn2 As Button

Private WithEvents btn3 As Button

Private WithEvents btn4 As Button

Private WithEvents btn5 As Button

Private WithEvents btn6 As Button

Private WithEvents btn7 As Button

Private WithEvents btn8 As Button

Private WithEvents btn9 As Button

Private WithEvents btn0 As Button


Private WithEvents btn1 As Button

Private WithEvents btn2 As Button

Private WithEvents btn3 As Button

Private WithEvents btn4 As Button

Private WithEvents btn5 As Button

Private WithEvents btn6 As Button

Private WithEvents btn7 As Button

Private WithEvents btn8 As Button

Private WithEvents btn9 As Button

Private WithEvents btn0 As Button

Private WithEvents btn1 As Button

Private WithEvents btn2 As Button

Private WithEvents btn3 As Button

Private WithEvents btn4 As Button

Private WithEvents btn5 As Button

Private WithEvents btn6 As Button

Private WithEvents btn7 As Button

Private WithEvents btn8 As Button

Private WithEvents btn9 As Button

Private WithEvents btn0 As Button

Private WithEvents btn1 As Button

Private WithEvents btn2 As Button

Private WithEvents btn3 As Button

Private WithEvents btn4 As Button

Private WithEvents btn5 As Button

Private WithEvents btn6 As Button

Private WithEvents btn7 As Button

Private WithEvents btn8 As Button

Private WithEvents btn9 As Button

Private WithEvents btn0 As Button

Private WithEvents btn1 As Button


Private WithEvents btn2 As Button

Private WithEvents btn3 As Button

Private WithEvents btn4 As Button

Private WithEvents btn5 As Button

Private WithEvents btn6 As Button

Private WithEvents btn7 As Button

Private WithEvents btn8 As Button

Private WithEvents btn9 As Button

Private WithEvents btn0 As Button

Private WithEvents btn1 As Button

Private WithEvents btn2 As Button

Private WithEvents btn3 As Button

Private WithEvents btn4 As Button

Private WithEvents btn5 As Button

Private WithEvents btn6 As Button

Private WithEvents btn7 As Button

Private WithEvents btn8 As Button

Private WithEvents btn9 As Button

Private WithEvents btn0 As Button

Private WithEvents btn1 As Button

Private WithEvents btn2 As Button

Private WithEvents btn3 As Button

Private WithEvents btn4 As Button

Private WithEvents btn5 As Button

Private WithEvents btn6 As Button

Private WithEvents btn7 As Button

Private WithEvents btn8 As Button

Private WithEvents btn9 As Button

Private WithEvents btn0 As Button

Private WithEvents btn1 As Button

Private WithEvents btn2 As Button


Private WithEvents btn3 As Button

Private WithEvents btn4 As Button

Private WithEvents btn5 As Button

Private WithEvents btn6 As Button

Private WithEvents btn7 As Button

Private WithEvents btn8 As Button

Private WithEvents btn9 As Button

Private WithEvents btn0 As Button

Private WithEvents btn1 As Button

Private WithEvents btn2 As Button

Private WithEvents btn3 As Button

Private WithEvents btn4 As Button

Private WithEvents btn5 As Button

Private WithEvents btn6 As Button

Private WithEvents btn7 As Button

Private WithEvents btn8 As Button

Private WithEvents btn9 As Button

' Operation buttons

Private WithEvents btnAdd As Button

Private WithEvents btnSub As Button

Private WithEvents btnMul As Button

Private WithEvents btnDiv As Button

Private WithEvents btnEquals As Button

Private WithEvents btnClear As Button

Private WithEvents btnDecimal As Button

' Calculator state

Private currentInput As String = "0"

Private previousInput As String = ""

Private currentOperation As String = ""


Private newNumber As Boolean = True

Public Sub New()

InitializeControls()

End Sub

Private Sub InitializeControls()

[Link] = "Simple Calculator"

[Link] = New Size(300, 400)

[Link] = [Link]

[Link] = New Font("Segoe UI", 10)

' Display

txtDisplay = New TextBox()

[Link] = New Point(20, 20)

[Link] = New Size(240, 30)

[Link] = [Link]

[Link] = True

[Link] = [Link]

[Link] = "0"

[Link](txtDisplay)

' Define button layout (row, column)

Dim buttons As Button() = New Button(16) {}

buttons(0) = CreateButton("7", 20, 70)

buttons(1) = CreateButton("8", 80, 70)

buttons(2) = CreateButton("9", 140, 70)

buttons(3) = CreateButton("/", 200, 70)

buttons(4) = CreateButton("4", 20, 120)

buttons(5) = CreateButton("5", 80, 120)


buttons(6) = CreateButton("6", 140, 120)

buttons(7) = CreateButton("*", 200, 120)

buttons(8) = CreateButton("1", 20, 170)

buttons(9) = CreateButton("2", 80, 170)

buttons(10) = CreateButton("3", 140, 170)

buttons(11) = CreateButton("-", 200, 170)

buttons(12) = CreateButton("0", 20, 220)

buttons(13) = CreateButton(".", 80, 220)

buttons(14) = CreateButton("=", 140, 220)

buttons(15) = CreateButton("+", 200, 220)

' Clear button (separate row)

btnClear = CreateButton("C", 20, 270)

[Link] = New Size(120, 40)

AddHandler [Link], AddressOf Clear_Click

[Link](btnClear)

' Add all buttons to the form and assign event handlers

For Each btn As Button In buttons

[Link](btn)

AddHandler [Link], AddressOf Button_Click

Next

' Store references to specific operation buttons for later use

btn0 = buttons(12) ' Actually 0 is at index 12, adjust as per layout above

btn1 = buttons(8)

btn2 = buttons(9)

btn3 = buttons(10)

btn4 = buttons(4)
btn5 = buttons(5)

btn6 = buttons(6)

btn7 = buttons(0)

btn8 = buttons(1)

btn9 = buttons(2)

btnDecimal = buttons(13)

btnEquals = buttons(14)

btnAdd = buttons(15)

btnSub = buttons(11)

btnMul = buttons(7)

btnDiv = buttons(3)

' Note: The order of buttons in the array doesn't match the digit names,

' but we use the event handler to identify by button text.

' The references above are just for clarity; we could also use DirectCast(sender,
Button).Text.

End Sub

Private Function CreateButton(text As String, x As Integer, y As Integer) As Button

Dim btn As New Button()

[Link] = text

[Link] = New Point(x, y)

[Link] = New Size(50, 40)

[Link] = [Link]

Return btn

End Function

' Common click handler for all digit and operator buttons

Private Sub Button_Click(sender As Object, e As EventArgs)

Dim btn As Button = DirectCast(sender, Button)

Dim buttonText As String = [Link]


Select Case buttonText

Case "0" To "9"

DigitPressed(buttonText)

Case "."

DecimalPressed()

Case "+", "-", "*", "/"

OperatorPressed(buttonText)

Case "="

EqualsPressed()

End Select

End Sub

Private Sub DigitPressed(digit As String)

If newNumber Then

currentInput = digit

newNumber = False

Else

If currentInput = "0" AndAlso digit <> "." Then

currentInput = digit

Else

currentInput &= digit

End If

End If

UpdateDisplay()

End Sub

Private Sub DecimalPressed()

If newNumber Then

currentInput = "0."

newNumber = False
ElseIf Not [Link](".") Then

currentInput &= "."

End If

UpdateDisplay()

End Sub

Private Sub OperatorPressed(op As String)

If Not newNumber Then

' If there's already an operation, compute it first

If currentOperation <> "" Then

EqualsPressed()

End If

previousInput = currentInput

currentOperation = op

newNumber = True

Else

' Allow changing operation without entering a new number

currentOperation = op

End If

End Sub

Private Sub EqualsPressed()

If currentOperation = "" OrElse newNumber Then

' No operation or no second number, do nothing

Return

End If

Dim first As Double

Dim second As Double

Dim result As Double


If [Link](previousInput, first) AndAlso [Link](currentInput, second)
Then

Select Case currentOperation

Case "+"

result = first + second

Case "-"

result = first - second

Case "*"

result = first * second

Case "/"

If second = 0 Then

[Link]("Cannot divide by zero.", "Error", [Link],


[Link])

ClearAll()

Return

End If

result = first / second

Case Else

Return

End Select

currentInput = [Link]()

previousInput = ""

currentOperation = ""

newNumber = True

UpdateDisplay()

Else

[Link]("Invalid input.", "Error", [Link],


[Link])

ClearAll()

End If

End Sub
Private Sub Clear_Click(sender As Object, e As EventArgs)

ClearAll()

End Sub

Private Sub ClearAll()

currentInput = "0"

previousInput = ""

currentOperation = ""

newNumber = True

UpdateDisplay()

End Sub

Private Sub UpdateDisplay()

[Link] = currentInput

End Sub

' Entry point (if running as standalone)

<STAThread>

Public Shared Sub Main()

[Link]()

[Link](False)

[Link](New Form1())

End Sub

End Class

You might also like