Lesson 4

Writing the Code

In Lesson 3, you learned how to place controls on the form. In this lesson, you will learn how to make those controls actually do something by writing Visual Basic 2015 code. You will understand event-driven programming, how to open the code window, how event procedures work, and how to write simple statements that respond to user actions.

Lesson focus:

Controls by themselves are not enough. You must write code for them so that they respond to events such as clicking, loading, typing, dragging, and more.

Lesson Overview

Lesson4
TopicWriting Code
Core IdeaEvent-Driven Programming
Main SkillUsing Event Procedures
Next StepWorking with Controls
4.1 Event-Driven Programming
4.2 The Code Window
4.3 MsgBox Example
4.4 Arithmetic Example

The Concept of Event-Driven Programming

Visual Basic 2015 is an event-driven programming language. This means your code runs when a specific event happens. If you place controls on a form but do not write any code for them, those controls will appear on the screen but they will not perform useful actions.

Every control on a form has a set of events related to it. Some common examples include the Load event, Click event, DoubleClick event, keyboard events, drag-and-drop events, and many others.

  • A form has its own events, such as Load.
  • A button has its own events, such as Click.
  • The same control can support many different events.
  • Your code is usually written inside an event procedure.
VB2015 Figure 4.1 Events associated with Form

Figure 4.1: Events associated with the form

VB2015 Figure 4.2 Events associated with the button control

Figure 4.2: Events associated with the button control

Opening the Code Window and Understanding Event Procedures

To start writing code in Visual Basic 2015, double-click a control or click part of the form to enter the code window. Visual Studio will generate an event procedure for you automatically. For the form, the default event is usually the Load event.

An event procedure begins with Private Sub and ends with End Sub. Between these two lines, you place the statements you want Visual Basic to execute when that event occurs.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

In the example above, the procedure runs when the form finishes loading. At first, it does nothing. To make it useful, you can add one or more statements inside it.

VB2015 Figure 4.3 The code window

Figure 4.3: The code window

Displaying a Message with MsgBox

One of the simplest ways to test your code is to display a message box. Visual Basic provides the built-in MsgBox function for this purpose. Place the following statement inside the form load event:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MsgBox("Welcome to Visual Basic 2015")
End Sub

When you run the program, the message box will appear automatically as soon as the form loads. This is a simple but powerful example of event-driven programming: the Load event triggers the code, and the code then displays the message.

VB2015 Figure 4.4 The popup message

Figure 4.4: The popup message

Understanding the Form Class

In Visual Basic 2015, the form is actually part of object-oriented programming. When you create a Windows Forms application, the default form appears as Form1. This is not just a screen; it is an object created from a form class. That is why you often see the line Public Class Form1 above your event procedures.

You do not need to master object-oriented programming immediately, but it is useful to know that your form and controls are objects, and those objects have properties, methods, and events.

Using Code for Arithmetic Calculations

Besides showing text, you can also use Visual Basic code to perform calculations. In the next example, the program adds two numbers and shows the result in a message box.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MsgBox("The answer is " & 1 + 2)
    Me.Close()
End Sub

In this example, the ampersand symbol & is used for string concatenation, which means combining text with another value. The statement Me.Close() tells the program to close the current form after the user clicks the OK button in the message box.

VB2015 Figure 4.5 Writing the code

Figure 4.5: Writing the code

VB2015 Figure 4.6 The output

Figure 4.6: The output of the arithmetic example

Why This Lesson Matters

Core takeaway:

Designing the form is only the first half of programming. Writing code gives life to the form and to every control you place on it. Once you understand events and event procedures, you can start creating interactive applications instead of static interfaces.

Build on This Foundation

Continue to VB2026

After learning the basics of event-driven coding in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.

Explore VB2026 →

Visual Basic Programming

Visual Basic Programming

Use this Top Release book to reinforce your tutorial learning with a more structured guide.

Exercise Questions

  1. What does it mean when we say Visual Basic 2015 is an event-driven programming language?
  2. What is the purpose of the Form1_Load event procedure?
  3. Write a short VB2015 example that displays a message box when a button is clicked.

Go to Lesson 5

In the next lesson, you will learn how to work with controls more effectively and use their properties and methods in real programs.