Visual Basic 2012 Lesson 5-Writing the Code

[Lesson 4]<< [CONTENTS] >> [Lesson 6]

In this lesson, you will learn some basic theories about Visual Basic 2012 programming. We shall focus more on learning by doing. We will keep the theories short so that it would not be too difficult for beginners.

Visual Basic 2012 is an object-oriented and event driven programming language. Event-driven means the VB program runs in response to the user’s action. The actions include clicking the command button, entering text in a text box, choosing an item, closing the application and more.

5.1 The Event Procedure

Each event is related to an object, it is an incident that happens to the object due to the action of the user. A class has events as it creates an instant of a class or an object. When we start a windows application in Visual Basic 2012, 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, as shown in the Form1 properties windows in Figure 5.1.

visual basic 2012 IDE

Figure 5.1

To start writing code in Visual Basic 2012,  click on any part of the form to go into the code window as shown in Figure 5.2. This is the structure of an event procedure. In this case, the event procedure is to load Form1 and it starts with Private Sub and ends with End Sub. This procedure includes the Form1 class and the event Load, and they are bind together with an underscore, i.e. Form_Load. It does nothing other than loading an empty form. You don’t have to worry the rest of the stuff at the moment, they will be explained in later lessons.

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<
End Sub
End Class

There are other events associated with the Form1 class, such as click, cursorChanged, DoubleClick, DragDrop, Enter and more, as shown in the diagram Figure 5.2 (It appears when you click on the upper right pane of the code window)

 Visual Basic 2012

Figure 5.2

5.2 Writing the code

Now you are ready to write the code for the event procedure so that it will do something more than loading a blank form. The code must be entered between Private Sub…….End Sub. Let’s enter the following code :

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 Me.Text = "My First VB2012 Program"
 Me.ForeColor = Color.LightGoldenrodYellow
 Me.BackColor = Color.RoyalBlue
End Sub
End Class

The first line of the code will change the title of the form to My First Visual Basic 2012 Program, the second line will change the foreground object to LightGoldenrodYellow( in this case, it is a label that you insert into the form and change its text to Foreground) and the last line changes the background to RoyalBlue color. The equal operator = in the code is used to assign something to the object, like assigning yellow color to the foreground of the Form1 object (or an instance of Form1). Me is the name given to the Form1 class. We can also call those lines as Statements. So, the actions of the program will depend on the statements entered by the programmer.

The output is shown Figure 5.3 below:

 Visual Basic 2012

Figure 5.3

Here is another example. In this project, you insert one button into the form and change its label text to “Display Hidden Names”. Click on this button to enter the code window and key-in the following code:

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name1, name2, name3, names As String
 name1 = "George"
 name2 = "Michael Chan"
 name3 = "Norman"
 names=name1 & ","& name2 & "and "& name3
 MsgBox(" The hidden names are " & names,,"Hidden Names")
End Sub

The keyword Dim is to declare variables name1, name2 and name3 , names as string, which means they can only handle text. The variable names is to accept the names that are joined together by the “&” signs as a single string. The function MsgBox  is to display the string in a message box, and the last argument “Hidden Names” is the title of the MsgBox function. The  output is shown in Figure 5.4 below:

Visual Basic 2012

Figure 5.4

[Lesson 4]<< [CONTENTS] >> [Lesson 6]