VB2022 VB2019 VB6 VB Sample Code About Us

Lesson 4 Writing the Code

In the previous lesson, we have learned how to design the UI by putting the controls(objects) on the form. However, they will not do anything unless we write code for them to perform some actions and to execute some functions. In this lesson, we will learn some basic coding in VB2019.

4.1 The Concept of Event-Driven Programming

According to Wikipedia, event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs or threads. VB 2019 is an event-driven programming language, therefore, every control has a set of events associated with it. Among the events are load, click, double click, drag, and drop, pressing the keys and more. To view the events, 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. You need to click on the default event to view other events associated with the control. The code appears on the left side is the event procedure associated with the Form load event. Figure 4.1 illustrates all the events associated with the default form.

Figure 4.1: Events associated with Form

Figure 4.2 shows the events associated with the button control.

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 as shown in Figure 4.1. This is the structure of an event procedure. In this case, the event procedure is loading Form1. It starts with Private Sub and ends with End Sub. This procedure includes the Form1 class and the event Load, and they are bound together with an underscore, i.e. Form_Load. It does nothing other than loading an empty form. To make the load event does something, insert the statement

MsgBox ("Welcome to Visual Basic 2019")
Figure 4.3: The code window

When you run the program, a message box that displays the text “My First Visual Basic 2019 Program” will appear, as shown in Figure 4.4. MsgBox is a built-in function in Visual Basic 2017 that displays a message in a pop-up message box.

Figure 4.4: The popup message

*You will notice that above Private Sub structure there is a preceding keyword Public Class Form1. This is the concept of an object-oriented programming language. When we start a windows application in Visual Basic 2019, we will see a default form with the name Form1 appears in the IDE, it is actually the Form1 Class that inherits from the Form class System.Windows.Forms.Form. A class has events as it creates an instance of a class or an object.

You can also write code to perform the arithmetic calculation. For example, you can use the MsgBox and the arithmetic operators to perform some arithmetic operations. Insert a button, click on it and enter the code as shown in Figure 4.5 :

Figure 4.5: Writing the code

*The symbol & (ampersand) is to perform string concatenation. Run the code and click the button, it will perform the calculation and display the result in a message box, as shown in Figure 4.6

Figure 4.6: Message Box

❮ Previous lesson Next lesson ❯

Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy