Writing The Code: #Topic3
Writing The Code: #Topic3
Visual Basic 2012 is an object oriented and event driven programming language.
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 the figure.
To start writing the code in VB 2012, click on any part of the form to go into the code
window. 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 underscore, i.e.
Form_Load. It does nothing other than loading an empty form.
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:
The first line of the code will change the title of the form to My First VB 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.
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: