VB2019 VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Code 中文VB About Us

Lesson 5 : Writing Code


In the previous lesson, you have learned that Visual Basic 2012 is an object oriented programming language. You have also understood the meanings of class, object, encapsulation inheritance as well as polymorphism. You have also learned to write some simple programs without much understanding some underlying foundations and theories. In this lesson, you will learn some basic theories about Visual Basic 2012 programming but we will focus more on learning by doing, i.e. learning by writing programs .I will keep the theories short so that it would not be too difficult for beginners.

5.1 The event Procedure

Visual Basic 2012 is an object oriented and event driven programming language. In fact, all windows applications are event driven. Event driven means the user will decide what to do with the program, whether he or she wants to click the command button,enter text in a text box, or close the application and etc. An event is related to an object, it is an incident that happens to the object due to the action of the user , such as a click or pressing a key on the keyboard. 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 in previous chapters, 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 properties window
Figure 5.1 Form1 properties windows

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 end 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



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