0% found this document useful (0 votes)
4 views6 pages

Programing Languge (Practical) Lecture 3

This document provides a tutorial for creating a simple calculator using Visual Basic .NET. It outlines the steps for designing a Windows Forms application, including adding controls for numeric input and operations, and writing event-driven code to perform arithmetic calculations. The tutorial includes specific code examples for handling button clicks and displaying results in a TextBox.

Uploaded by

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

Programing Languge (Practical) Lecture 3

This document provides a tutorial for creating a simple calculator using Visual Basic .NET. It outlines the steps for designing a Windows Forms application, including adding controls for numeric input and operations, and writing event-driven code to perform arithmetic calculations. The tutorial includes specific code examples for handling button clicks and displaying results in a TextBox.

Uploaded by

Dastan Faruq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming Language

Lecture 3

Dastan Faruq Ismail


dastan. faruq 1995@gmail .com

1
In this tutorial, we will create a simple
calculator using Visual Basic .NET.
• Steps include:
• Designing the form in Windows Forms
• Writing event-driven code for button clicks
• Performing arithmetic operations
• Displaying results in a TextBox

Step 1: Create a New Windows Forms App


1. Open Visual Studio.

2. Click Create a new project.

3. Select Windows Forms App (.NET Framework).

4. Click Next, name the project (e.g., CalculatorApp), and click


Create.

Step 2: Design the Calculator UI


1. Add Controls from the Toolbox
 One TextBox → To display numbers (txtDisplay).

 Ten Buttons (0–9) → For numeric input.

 *Four Buttons (+, -,* , /) → For operations.

 One "=" Button → To calculate the result.

 One "C" Button → To clear the display.

2
2. Arrange the Controls
Control Name Text Property
Property

TextBox txtDisplay (Empty)

Button btn1 1

Button btn2 2

Button btn3 3

Button btn4 4

Button btn5 5

Button btn6 6

Button btn7 7

Button btn8 8

Button btn9 9

Button btn0 0

Button btnAdd +

Button btnSubtract -

Button btnMultiply *

Button btnDivide /

Button btnEquals =

Button btnClear C

3
Step 3: Write the Code
Public Class Form1
Dim num1, num2 As Double
Dim operation As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load

End Sub

Private Sub btn1_Click(sender As Object, e As EventArgs) Handles


btn1.Click
txtdisply.Text &= "1"
End Sub

Private Sub btn2_Click(sender As Object, e As EventArgs) Handles


btn2.Click
txtdisply.Text &= "2"
End Sub

Private Sub btn3_Click(sender As Object, e As EventArgs) Handles


btn3.Click
txtdisply.Text &= "3"
End Sub

Private Sub btn4_Click(sender As Object, e As EventArgs) Handles


btn4.Click
txtdisply.Text &= "4"

4
End Sub

Private Sub btn5_Click(sender As Object, e As EventArgs) Handles


btn5.Click
txtdisply.Text &= "5"
End Sub

Private Sub btn6_Click(sender As Object, e As EventArgs) Handles


btn6.Click
txtdisply.Text &= "6"
End Sub

Private Sub btn7_Click(sender As Object, e As EventArgs) Handles


btn7.Click
txtdisply.Text &= "7"
End Sub

Private Sub btn8_Click(sender As Object, e As EventArgs) Handles


btn8.Click
txtdisply.Text &= "8"
End Sub

Private Sub btn9_Click(sender As Object, e As EventArgs) Handles


btn9.Click
txtdisply.Text &= "9"
End Sub

Private Sub btn0_Click(sender As Object, e As EventArgs) Handles


btn0.Click
txtdisply.Text &= "0"
End Sub

Private Sub btndot_Click(sender As Object, e As EventArgs) Handles


btndot.Click
txtdisply.Text &= "."
End Sub

Private Sub btnadd_Click(sender As Object, e As EventArgs) Handles


btnadd.Click
num1 = Val(txtdisply.Text)
operation = "+"
txtdisply.Clear()

End Sub

Private Sub btnsub_Click(sender As Object, e As EventArgs) Handles


btnsub.Click
num1 = Val(txtdisply.Text)
operation = "-"
txtdisply.Clear()
End Sub

Private Sub btnmultiply_Click(sender As Object, e As EventArgs) Handles


btnmultiply.Click
num1 = Val(txtdisply.Text)
operation = "*"
txtdisply.Clear()
End Sub

Private Sub btndivide_Click(sender As Object, e As EventArgs) Handles


btndivide.Click
num1 = Val(txtdisply.Text)

5
operation = "/"
txtdisply.Clear()
End Sub

Private Sub btnequle_Click(sender As Object, e As EventArgs) Handles


btnequle.Click
num2 = Val(txtdisply.Text)
Select Case operation
Case "+"
txtdisply.Text = (num1 + num2).ToString
Case "-"
txtdisply.Text = (num1 - num2).ToString
Case "*"
txtdisply.Text = (num1 * num2).ToString
Case "/"
txtdisply.Text = (num1 / num2).ToString

End Select
End Sub

Private Sub btnclear_Click(sender As Object, e As EventArgs) Handles


btnclear.Click
txtdisply.Clear()
num1 = 0
num2 = 0
End Sub
End Class

You might also like