Lesson 4: Writing the Code in VB2019
Learn the basics of event-driven programming and writing your first VB2019 code
Key Takeaway
Visual Basic 2019 uses event-driven programming where the flow of the program is determined by user actions. This lesson teaches you how to write code that responds to events like button clicks and form loads.
In the previous lesson, we designed the UI by placing controls on the form. However, they won't do anything unless we write code for them to perform actions. This lesson covers the basics of coding in VB2019.
4.1 The Concept of Event-Driven Programming
Event-driven programming is a programming paradigm where the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs. VB2019 is an event-driven language, meaning every control has a set of events associated with it.
Event-Driven Programming
In VB2019, code is executed in response to events. The most common events include:
- Load: Triggered when a form is loaded
- Click: Triggered when a control is clicked
- DoubleClick: Triggered when a control is double-clicked
- KeyPress: Triggered when a key is pressed
To view events associated with a control, double-click the control on the form to enter the code window. The default event will appear at the top part on the right side of the code window.

Figure 4.2 shows the events associated with the button control:

4.2 Writing the Code
To write code in Visual Basic 2019, click on any part of the form to enter the code window. This is the structure of an event procedure:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' This is the Form Load event handler ' Code placed here runs when the form loads End Sub End Class
This procedure starts with Private Sub and ends with End Sub. It includes the Form1 class and the Load event, bound together with an underscore.
To make the load event do something, insert the statement:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Display a welcome message MsgBox("Welcome to Visual Basic 2019") End Sub End Class
Output:
When you run the program, a message box displaying "Welcome to Visual Basic 2019" will appear. MsgBox is a built-in function in VB2019 that displays a message in a pop-up box.

Performing Calculations
You can also write code to perform arithmetic calculations. For example, you can use MsgBox with arithmetic operators:
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' Perform arithmetic calculations MsgBox("The sum of 5 and 3 is " & (5 + 3) & vbCrLf & _ "The product of 5 and 3 is " & (5 * 3)) End Sub End Class
Output:
The symbol & (ampersand) performs string concatenation. Run the code and click the button to see the calculation results:

Important Note
You'll notice above the Private Sub structure there is a preceding keyword Public Class Form1. This is VB2019's object-oriented programming concept. When we start a Windows application, the default form (Form1) is actually a class that inherits from the Form class System.Windows.Forms.Form.
Lesson Summary
In this lesson, you've learned the fundamentals of writing VB2019 code:
Event-Driven Programming
Understand how events determine program flow in VB2019
Event Handlers
Create procedures that respond to events like form load and button clicks
MsgBox Function
Use built-in functions to display messages to users
Arithmetic Operations
Perform calculations and display results
You've learned how to write basic VB2019 code that responds to events. In the next lesson, we'll explore how to work with controls programmatically.
Next Lesson
Ready to learn how to work with controls? Continue to Lesson 5: Working with Controls.
Related Resources

Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.

Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.