0% found this document useful (0 votes)
54 views

Writing The Code: #Topic3

The document discusses writing code for event procedures in Visual Basic 2012. It explains that VB 2012 is event-driven, meaning the user triggers events like button clicks or text entry. An event procedure contains the code that runs for a specific event tied to an object. For example, the Form1_Load event procedure contains code that runs when the Form1 form loads. The document provides an example of code written in the Form1_Load event procedure to change the form's title, foreground, and background colors. It also gives an example of code written in the Button1_Click event procedure to display hidden names in a message box when the button is clicked.

Uploaded by

Jimuel Ladao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Writing The Code: #Topic3

The document discusses writing code for event procedures in Visual Basic 2012. It explains that VB 2012 is event-driven, meaning the user triggers events like button clicks or text entry. An event procedure contains the code that runs for a specific event tied to an object. For example, the Form1_Load event procedure contains code that runs when the Form1 form loads. The document provides an example of code written in the Form1_Load event procedure to change the form's title, foreground, and background colors. It also gives an example of code written in the Button1_Click event procedure to display hidden names in a message box when the button is clicked.

Uploaded by

Jimuel Ladao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#Topic3

WRITING THE CODE


The Event Procedure

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.

Public Class Form1


Private Sub Form1_Load(ByVal sender As System.Object, ByVal As
System.EventArgs) Handles MyBase.Load
End Sub
End Class
There are other events associated with the Form1 class, such as click, cursor
Changed, DoubleClick, Drag Drop, Enter and more. (It appears when you click on the upper
right pane of the code window).

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

Private Sub Button1_Click_1 (ByVal sender As System.Object, ByVal As


System.EventArgs) Handles Button1.Click
Dim name1, name2, name3, names As String
name1 = “George”
name2 = “Michael”
name3 = “Norman”
names = name1 &”, “& name2 &” and “& name3
MsgBox (“The hidden names are “ & names, , “Hidden Names”)
End Sub
End Class

You might also like