In this lesson, we shall learn how to write code for all the controls enabling them to interact with the events triggered by the users. Before learning how to write Visual Basic 2022 code, let us delve into the concept of event-driven programming.
Visual Basic 2022 is an event-driven programming language, meaning that the code is executed in response to events triggered by the user like clicking the mouse or pressing a key on the keyboard. Some other events are selecting an item from a drop-down list, typing some words into a text box and more. It may also be an event that responds to some other events. Some of the common events in Visual Basic 2022 are load, click, double-click, drag-drop, keypress and more. Every control you place on the form has a set of events associate with it. To view the events, double-click the control on the form to enter the code window. The default event will appear at the top right side of the code window. You must click 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 load event. Figure 4.1 illustrates the event procedure Load associated with the Form and Figure 4.2 shows the events associated with the button.
Figure 4.2 shows the events associated with the button control.
To write code in Visual Basic 2022, 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, write the following code:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles_ MyBase.Load MsgBox ( "My First Visual Basic 2022 APP", ,"My Message") End Sub End Class
When you run the program, a message box that displays the text “My First Visual Basic 2022 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.
*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 2022, 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.
Now, let us write a code that perform arithmetic calculation, as in Example 4.1:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load MsgBox("2" & "+" & "5" & "=" & 2 + 5) End Sub
*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.4