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.

Visual Basic 2019 IDE Events
Figure 4.1: Events associated with Form

Figure 4.2 shows the events associated with the button control:

Button control events
Figure 4.2: 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:

Form1.vb
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:

Form1.vb
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.

Message Box Output
Figure 4.3: The popup message

Performing Calculations

You can also write code to perform arithmetic calculations. For example, you can use MsgBox with arithmetic operators:

Form1.vb
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:

Calculation Output
Figure 4.4: Message Box with 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

VB6 Tutorial

Mastering VB6 Programming

Explore Tutorials

Visual Basic Examples

Practical VB code samples for real-world applications

View Examples

Excel VBA Tutorial

Learn how to automate Excel by creating VBA macros

Learn More

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon