[Lesson 3] << [Contents] >> [Lesson 5]
In the preceding lesson, you have learned how to design the user interface by inserting controls to the form and changing their properties. However, the controls will not function without adding code to them. Therefore, we shall learn how to write code for all the controls so that they can interact with events triggered by the users. However, before learning how to write Visual Basic code, let me first explain the concept of event-driven programming
4.1 The Concept of Event-Driven Programming
Visual Basic 2013 is an event-driven programming language, which means that the code is executed in response to events. In addition, every control(object) you place on the form has a set of events related to them. Some of the events are load, click, double click, drag, and drop, pressing the keys and more.
In order to view the events, you need to double-click the control on the form to enter the code window. The default event will appear at the top portion on the right side of the code window. In addition, you need to click on the default event to view other events associated with the control. As you shall see, the VB 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 default form.
Figure 4.1: Events associated with Form
Figure 4.2 shows the events associated with the button
Figure 4.2
4.2 Writing the VB Code
After opening up the code window, you can start writing the VB code. The procedure in this example is to load the default form Form1. As you can see, the control Form1 is associated with the Load event with an underscore, i.e. Form1_Load. It does nothing other than loading an empty form. Furthermore, the event procedure usually starts with the keywords Private Sub and ends with the keywords End Sub. In order to make the load event does something, we shall insert the statement MsgBox( “Welcome to Visual Basic 2013”)
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MsgBox( "My First Visual Basic 2013 Program") End Sub End Class
When you run the program, a message box that displays the text “My First Visual Basic 2013 Program” will appear, as shown in Figure 4.3. MsgBox is a built-in function in Visual Basic 2013 that displays a message in a pop-up message box.
4.2.1 The Concept Object-Oriented Programming
You will notice that in the event procedure illustrated above, there is a preceding keyword Public Class Form1. This is the concept of object-oriented programming language. When we start a windows application in Visual Basic 2013, 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. We shall learn more about object-oriented programming in another lesson.
Example 4.1
You can also write VB code to perform arithmetic calculations. For example, you can use the MsgBox and the arithmetic operator plus to perform an addition of two numbers, as shown below:
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.
The output is as shown in Figure 4.4
If you wish to close the window after the message, you can add the statement Me.Close(), as follows:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load MsgBox("2"& "+"& "5"&"=" & 2 + 5) Me.Close End Sub
We will learn more about code writing in coming lessons