technology and livelihood education iv
DESCRIPTION
Lesson 4 – Objects: Properties, Methods and EventsTRANSCRIPT
CHAPTER 1: INTRODUCTION TO THE VISUAL BASIC
LANGUAGE AND ENVIRONMENT
•Understand what is an object•Learn the properties, methods and events that correspond with an object•Learn the use of the Properties Window
Lesson 4 – Objects: Properties, Methods and
EventsOBJECTIVES
Introduction
Objects are a distinct unit in a program that has a
certain characteristics and can perform certain actions.
Everything in Visual Basic revolves around what is
called as OBJECTS.
Caption Property
Back Color Property
OBJECT PROPERTIESObjects have PROPERTIES, just like a real-world
object, properties determines what physical appearance your object will look like. You can
change the different properties of each objects.
The format for using Properties in an object is:
ObjectName.Property=Value
Object Name = Name of the object with the properties to change
Property = attribute you want to change
Value = New data of the property
EXAMPLE
Form1.Caption=“Welcome”
ObjectName
Property
Value
Properties Window
• You can also change the Property of an object at design time by using the Properties Window in Visual Basic.
OBJECT METHODSObjects can also perform functions of their own.
The whole point of creating objects is that objects does something useful. This additional
functions are known as METHODS. This allows the objects to perform certain actions.
The format for using Properties in an object is:
ObjectName.Method Value
Object Name = Name of the object with the properties to change
Method = action to perform
Value = Optional parameters or data you can add
EXAMPLE
Form1.Show
ObjectName
Method
EXAMPLE
Combo1.AddItem “IV-Malory”
ObjectName
To add data to a ComboBox we can use the methods optional parameter
Properties or Methods are
separated from the object by using a dot
Method
Optional Parameter/value
USING THE FORMThe form is the basic repository for all you controls, this is were you add all those controls when you design the user
interface. A program can have one or more forms. The form that first appear when you run you program is called
the startup form.
You can change the order of the startup of each form by:
1. Clicking Project from the menu bar
2. Select Project1 Properties
3. The Project Properties will appear
4. Click the Startup Object combo box
5. Select from the list of forms
6. Then click the OK button
USING THE PROPERTIES WINDOW
Each form and control has properties assigned to it by default when you start up a new project. There are two ways to display the properties of an object. The first way is to click on the object (form or control) in the form window. Then, click on the Properties Window or the Properties Window button in the tool bar. The second way is to first click on the Properties Window. The select the object from the Object box in the Properties Window.
Properties can be viewed in two ways: • Alphabetic• Categorized
THE PROPERTIES WINDOW
Each object has a set of attributes, called properties, that determine its appearance and behavior
The Properties window exposes the object’s properties to the programmer
The Properties window includes an Object box and a Properties list
The Properties window can be used to change a property of an object
THE PROPERTIES WINDOW
Properties window showing the properties of the Form1.vb file object
PROPERTIES OF THE WINDOWS FORM OBJECT
The Windows Form object has a set of properties
Properties of the Windows Form object will appear in the Properties window when you select the Windows Form object in the designer window
PROPERTIES OF THE WINDOWS FORM OBJECT
Important properties of the Windows Form object:Name propertyText propertyStartPosition propertySize propertyBackgroundImage property
SETTING PROPERTIES AT RUN TIMEYou can also set or modify properties
while your application is running . To do this, you must write some code. The
code format is:
ObjectName.Property = NewValueSuch a format is referred to as dot
notation. For example, to change the BackColor property of a form name
Form1, we’d type:
Form1.BackColor = vbBlue
HOW NAMES ARE USED IN OBJECT EVENTSThe names you assign to objects are used by Visual Basic to set up a
framework of event-driven procedures for you to add code to. The format for
each of these subroutines (all object procedures in VB are subroutines) is:
Sub ObjectName_Event (Optional Arguments)
End Sub
VB provides the Sub line with its arguments (if any) and the End Sub
statement. You provide any needed code.
TRIGGERING EVENTS ON FORM
Several form events will fire starting with the moment of creation. Each has its own purpose and triggers in a
certain order. When the Form is first created, an initialize event will occur, followed by Load, Resize, and Paint.
1. Start VB and create a new project
2. Double-click the form
3. The Code Window will appear, by default you’ll be in the Load event procedure, click on the events combo box, scroll through and select the Click event.
4. You’ll now have two event procedure (Load and Click)
5. Click inside the Form_Click event and type the following statement
6. Your code window should look like the one in the next slide.
Private Sub Form_Click()Msg “Welcome to Malory”
End Sub
Event Procedure
Program Statement
Properties
Description
Caption Changes the caption of the title bar in the form Window.
Appearance Changes the 3D or Flat appearance of the form
BackColor Changes the form background color
Font Changes the Font style of the form
Enable Sets True of False boolean data, disables and enable the form
Picture Puts a picture on the form
Visible Hide or unhide the form by setting it True or False
ForeColor Changes the color of the text
Events DescriptionLoad Triggers once the program starts
Unload Triggers once the program exits
Activate Triggers once the form is active
Click Triggers once the form is click
Double Click Triggers once the form is double click
Terminate Like Initialize, this event will only occur during the existing of the Form. It won’t fire if the program is terminated abnormally
Initialize This event occurs only once when the Form is created. It will not fire again unless the Form is Unloaded and Loaded again
Methods DescriptionHide Hides the active form
Unhide Unhide the active form
Print Use to print text on the form
Cls Clear any picture on the form
IV-MALORYSubmitted by: Group 4