VB.
NET Basics + Intro to
Windows Forms
Learning Objectives
• Understand the structure of a Windows Forms project
• Learn how to add and use controls (Button, Label, TextBox)
• Understand event-driven programming (Button.Click)
• Apply basic variables and simple logic in a form
Windows Forms Project Structure
• Solution Explorer shows:
– Form1.vb – Your main form
– Form1.Designer.vb – Auto-generated UI code
– Program.vb / ApplicationEvents.vb – Entry point
• Forms are graphical windows where users interact with your
program
Common Controls
• Label – Displays text
• TextBox – Accepts user input
• Button – Performs actions on click
• PictureBox – Displays images
• DataGridView – Displays tables/lists
Event-Driven Programming
• Your program reacts to user actions
• Example: Button Click, Text Change, Form Load
Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
MessageBox.Show("Button clicked!")
End Sub
Declaring Variables in Forms
Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
Dim name As String = TextBox1.Text
Dim age As Integer = CInt(TextBox2.Text)
MessageBox.Show("Hello " & name & "! Next year you will be "
& (age + 1))
End Sub
• Dim – Declare variable
• TextBox.Text – Reads user input
• MessageBox.Show() – Displays output
Activity
Create a simple program that will add two numbers:
> Create a form with:
1. 2 TextBoxes → txtNumber1, txtNumber2
– For user to enter two numbers
2. 1 Button → btnAdd,
– To perform a math operation (Add)
3. 1 Label → lblResult
– Displays the calculation result
Activity (Answer)
Private Sub btnAdd_Click(sender As Object, e As EventArgs)
Handles btnAdd.Click
Dim num1 As Double = CDbl(txtNumber1.Text)
Dim num2 As Double = CDbl(txtNumber2.Text)
lblResult.Text = "Sum: " & (num1 + num2)
End Sub