Lesson 4: Writing the Code in VB2022
Master event-driven programming and learn to write code that responds to user interactions
Key Takeaway
Visual Basic 2022 is an event-driven programming language. Your code responds to events triggered by user actions like clicking buttons or typing in textboxes. Mastering event handling is essential for creating interactive applications.
Welcome to Lesson 4 of our Visual Basic 2022 Tutorial! In this lesson, you'll learn how to write code that makes your applications respond to user interactions. We'll cover event-driven programming concepts, create event handlers, and explore practical examples that bring your UI to life.
Learning Objectives
- Understand event-driven programming concepts
- Write code for different events
- Use message boxes for user feedback
- Perform arithmetic operations
- Work with form controls programmatically
4.1 The Concept of Event-Driven Programming
Visual Basic 2022 is an event-driven programming language, meaning code executes in response to events triggered by user actions like clicking a mouse, pressing keys, or selecting items from lists. Other events include form loading, mouse movements, and timer events.
Pro Tip
The most common events you'll use include Click
for buttons, TextChanged
for textboxes, and Load
for forms.
1 Accessing Events
Every control on a form has a set of associated events. To view these events:
- Double-click a control on the form to open the code window
- View the default event at the top right of the code window
- Click the default event dropdown to see all available events
2 Event Procedure Structure
All event procedures follow this basic structure:
Private Sub ControlName_EventName(sender As Object, e As EventArgs) Handles ControlName.EventName ' Your code goes here End Sub
The sender
parameter refers to the object that raised the event, and e
contains event-specific data.


4.2 Writing Your First Code
Let's start with simple examples that demonstrate how to write and execute code in VB2022.
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load MsgBox("My First Visual Basic 2022 App", , "Welcome Message") End Sub End Class
Output:
When you run this program, a message box displaying "My First Visual Basic 2022 App" will appear as soon as the form loads.

Understanding MsgBox
The MsgBox
function displays a message in a dialog box. The parameters are:
- Message text
- Buttons (optional - omitted here)
- Title (optional)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load MsgBox("2 + 5 = " & (2 + 5)) MsgBox("10 - 3 = " & (10 - 3)) MsgBox("4 × 6 = " & (4 * 6)) MsgBox("20 ÷ 5 = " & (20 / 5)) End Sub
Output:
This example demonstrates basic arithmetic operations in VB2022. When the form loads, it will show four message boxes with different calculations.

Important Note
The &
operator concatenates strings. Numeric operations are performed inside parentheses and converted to strings for display.
4.3 Practical Examples
Button Click Event
This example shows how to handle a button click event to change a label's text.
1 Implementation Steps
- Add a Button control to your form (named Button1)
- Add a Label control (named Label1)
- Double-click Button1 to create the Click event handler
2 Code Example
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Label1.Text = "Button was clicked!" Label1.ForeColor = Color.Blue Label1.Font = New Font("Arial", 12, FontStyle.Bold) End Sub
Behavior: When Button1 is clicked, the label text changes to "Button was clicked!", turns blue, and uses a bold Arial font.


Text Input Processing
vb2022/vb2022images/Figure4.5a;pngThis example demonstrates how to get user input from a TextBox and display it in a message box.
1 Implementation Steps
- Add a TextBox control (named TextBox1)
- Add a Button control (named Button2)
- Double-click Button2 to create the Click event handler
2 Code Example
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click If String.IsNullOrEmpty(TextBox1.Text) Then MsgBox("Please enter your name!", MsgBoxStyle.Exclamation, "Input Required") Else MsgBox("Hello, " & TextBox1.Text & "! Welcome to VB2022.", MsgBoxStyle.Information, "Greeting") End If End Sub
Behavior:
- If TextBox1 is empty, shows an error message
- If text is entered, shows a personalized greeting
Lesson Summary
In this lesson, we covered essential concepts for writing code in Visual Basic 2022:
Event-Driven Programming
VB2022 executes code in response to user-triggered events
Event Procedures
Structured as Private Sub ControlName_EventName(...) Handles ...
Message Boxes
Using MsgBox() to display information to users
Arithmetic Operations
Performing calculations with +, -, *, / operators
Control Interaction
Changing properties of controls like Labels and TextBoxes
These skills form the foundation for creating functional applications in VB2022. In the next lesson, we'll explore how to work with various controls in more depth.
Exercises
Practice what you've learned with these exercises:
Exercise 1: Simple Calculator
Create a program with:
- Two TextBox controls for number input
- Four Button controls (+, -, ×, ÷)
- One Label to display results
Write code so that when a button is clicked, the corresponding arithmetic operation is performed on the two numbers, and the result is displayed in the Label.
Exercise 2: Personal Information Form
Create a form with:
- Textboxes for first name, last name, and age
- A "Submit" button
When the button is clicked:
- Validate that all fields contain data
- If valid, display a message: "Hello [First Name] [Last Name]. You are [Age] years old."
- If invalid, show an appropriate error message
Exercise 3: Dynamic Control Properties
Create a program that allows users to:
- Change a label's text using a TextBox and Button
- Change a label's color using a ComboBox with color options
- Toggle a label's visibility with a CheckBox
Next Lesson
Ready to learn how to work with different controls? Continue to Lesson 5: Working with Controls.
Related Resources

Visual Basic 2022 Made Easy
The ultimate beginner-friendly guide for mastering Windows-based application development using Visual Basic in Visual Studio 2022. Whether you're a student, teacher, hobbyist, or self-learner, this book offers a clear, step-by-step approach to help you get started with ease.
What You'll Learn:
- Introduction to the Visual Studio 2022 IDE
- Working with variables, data types, loops, and procedures
- Designing forms and creating user-friendly interfaces
- Using controls such as buttons, text boxes, and combo boxes
- Menus, dialog boxes, file handling, and more

Mastering Excel VBA 365
Your ultimate step-by-step guide to automating tasks, building macros, and creating powerful applications within Microsoft Excel 365. Whether you're a student, business professional, or aspiring programmer, this comprehensive handbook will help you unlock the full potential of Excel's VBA.
What You'll Learn:
- Write and debug efficient VBA code
- Use controls, loops, and conditional logic
- Automate repetitive Excel tasks
- Handle errors and create UserForms
- Work with Excel objects and charts