MIS288 Lecture 8

Download Report

Transcript MIS288 Lecture 8

Windows Software Development
Lecture 8
MIS288
Instructor – Larry Langellier
Where Are We?

Last Lecture
• Creating a Collection Hierarchy
•
•
•
•
•

Create a collection using the Collection object
Create an object hierarchy
Implement default properties and methods
Working with Project Groups
Enumerate a collection – For Each
Tonight – Creating an ActiveX Control
• Create an ActiveX control from constituent controls
• Understand design time and run time when
developing ActiveX controls
• Create ActiveX controls that support Property
Pages
ActiveX Control Creation

Visual Basic supports ActiveX control
creation by:
• Enhancing existing controls
• Creating controls from multiple existing controls
• Creating controls from scratch
Container Terminology




Control instances must exist inside a
container
Containers are not unique to Visual
Basic but general to Windows
Controls and their container interact
A VB Form window is but one example
of a container
• Internet Explorer is a container
Creating an ActiveX Control

Create an ActiveX control project type
• New Project -> ActiveX Control



Name property of an ActiveX control is
the name known to the developer
ToolBoxIcon contains the icon that
appears in the toolbox
Public property defines whether another
project can create a control instance
Design time vs. run time

An Author develops and maintains an ActiveX
control
 The Developer uses the ActiveX control
created by the Author
• In the past you have always assumed the role of
developer in relation to controls

The meaning of design time and run time is
expanded when creating an ActiveX Control
 ActiveX control can be in design or run mode
• When visual designer is open, the ActiveX control is
in design mode
• When visual designer is closed, the ActiveX control
instance is running

Standard EXE project can be in design mode
or run mode
ActiveX Control Objects


UserControl is the core object just as
the Form object is the core of a form
Several other objects provide support to
the ActiveX Control
• The Container suggests initial values to a
control through the AmbientProperties
object
• Properties defined by the container are
available through the Extender object
• The PropertyBag object makes properties
persistent
UserControl Interaction
UserControl object
Developer
User control
properties
Extender Object
Public Property
procedures
(properties)
Public Sub and
Function
procedures
(methods)
Constituent control
(TextBox)
PropertyBag object
Author
Predefined Extender
Properties and Events




Supports the Cancel property if
DefaultCancel is set to True
Parent property (at run time) contains a
reference to the container
TabStop and TabIndex properties
appear if UserControl’s CanGetFocus
property is True
GotFocus and LostFocus events occur
only if the UserControl’s CanGetFocus
property is True
Predefined Ambient
Properties




TextAlign property defines alignment of
text
UserMode property indicates whether
the control is in design mode or run
mode
Font contains suggested font
BackColor and ForeColor properties
suggest colors
Ways to create an
ActiveX control

Enhance an existing control
Create a control based upon multiple
existing controls

Create a control from scratch

• Difficult in Visual Basic
• Other two methods are preferable
Constituent Controls




Create an ActiveX control by enhancing
an existing control
Existing control is called a Constituent
control
Properties, methods, and events of
constituent control are hidden from the
developer and must be explicitly
exposed
The Author may more than one
constituent control instance on the User
control
User Control Events
User controls respond to events – but the
events of the constituent controls are hidden
 Therefore, the Author writes code to respond
to these events

• The response varies depending on Mode
• Example: Clicking on a CommandButton in Design Mode
allows the developer to resize or move it, while the user
clicking during Run Mode generates a Click event
• Resize event occurs when the developer resizes
the ActiveX control instance
• What should the author do if more than one constituent
control is involved (like the TextBoxPlus)?
– Adjust the width of both to fill the region (with a minimum)
– Fixed height for Label, TextBox fills the rest

Example – Ch11_C.vbg
• TextBoxPlus control – UserControl_Resize
User Control Event
Sequences

Developer creates a control instance
• Initialize, InitProperties, Resize, Paint

Switch from design mode to run mode
• WriteProperties, Terminate (Design time instance),
Initialize (Run time instance), ReadProperties,
Resize, Paint

Switch from run mode to design mode
• Terminate (Run time instance), Initialize (Design
time instance), ReadProperties, Resize, Paint

Developer exits Visual Basic
• WriteProperties, Terminate

User runs executable program containing
control instance
• Initialize, ReadProperties, Resize, Paint
Object Focus

Examine GotFocus event
• Container raises GotFocus when the object
(ActiveX control instance) gets focus
• If no constituent control can get focus,
GotFocus event pertaining to UserControl
is raised
• If a constituent control can get focus,
GotFocus event pertaining to the
constituent control is raised
User Control Focus
Events





EnterFocus event occurs when the
UserControl or constituent control gets focus
GotFocus occurs next (if there are no
constituent controls)
When the User control loses focus, the
LostFocus event occurs (if there are no
constituent controls)
ExitFocus event occurs when UserControl
loses focus
EnterFocus and ExitFocus always occur,
regardless of whether a constituent control
can get focus
Raising an Event for the
Developer

Event must be declared in the User control
module
• Event statement

Event must be raised in the User control
module
• Raise statement
At run time, user clicks a
constituent text box
Click event occurs for the
constituent text box.
Visual Basic raises the Click
event.
UserControl object
Public Event Click()
Private Sub Text1_Click()
RaiseEvent Click
End Sub
Form Module
Private Sub tbpName_Click
Debug.Print "Clicked"
End Sub
Techniques to Expose
Properties to the Developer

Three techniques
• Delegation
• Expose a property of a constituent control
• Aggregation
• Group the properties of more than one
constituent control
• Create a new property

Do NOT
• Modify the behavior of an existing property
PropertyChanged method

Advise the container that the value of a
property has changed
• Necessary so the Properties window value can be
updated or the control instance on the form can be
drawn with the new property value

Call PropertyChanged whenever setting a
property
 Syntax
object.PropertyChanged PropertyName

Public variables should not be used to
implement ActiveX control properties
Delegation

Constituent control property is exposed
to the developer
• Text1.Enabled property is delegated
Public Property Get Enabled() As Boolean
Enabled = Text1.Enabled
End Property
Public Property Let Enabled ( _
ByVal New_Enabled As Boolean)
Text1.Enabled = New_Enabled
PropertyChanged "Enabled"
End Property
Aggregation

Properties from multiple constituent
controls aggregated into one property
Public Property Get FontSize() As Integer
FontSize = Text1.FontSize
End Property
Public Property Let FontSize( _
ByVal New_Size As Integer)
Text1.FontSize = New_Size
Text2.FontSize = New_Size
PropertyChanged "FontSize"
End Property
Property Persistence



Remember that a control’s run time
instance is different than the design
time instance
You must explicitly make a property
persistent between design time and run
time
Container saves property - not the
control itself
The PropertyBag object

PropertyBag object reads and writes
persistent properties
• The Container reads/writes the PropertyBag

Methods
• ReadProperty method reads a property from the
PropertyBag
• WriteProperty method saves a property to the
property bag

UserControl Events
• InitProperties occurs when the control is first
created
• WriteProperties occurs before a design time
instance is destroyed
• ReadProperties occurs when creating a
preexisting design time control instance
ReadProperties Example

Read the value of the Enabled property
from the PropertyBag
Private Sub UserControl_ReadProperties _
(PropBag As PropertyBag)
Text1.Enabled = PropBag.ReadProperty _
("Enabled", True)
End Sub
Property Name
Value used if no entry
in PropertyBag
WriteProperties Example

Save the Enabled property to the
PropertyBag
Private Sub UserControl_WriteProperties _
(PropBag As PropertyBag)
Call PropBag.WriteProperty _
("Enabled", Text1.Enabled,True)
End Sub
Property Name
Default value
Types of Properties




Read-write properties
Read-only properties
Write-Once properties
Property may be hidden depending on
the run state
Just Do It!




Work on Exercise 11.2 from the
textbook (pages 678-679)
There is an executable demo (JDI1.exe)
located in the Classroom Lecture
Demos – feel free to run that to get a
better idea of what you’re being asked
to do
We will discuss the solution after you’ve
worked on problem for a while
Call me over if you have questions –
don’t sit there stumped for long
Special Property Types



OLE_COLOR
Boolean Values
Enumerated types
• Intrinsic
• User-Defined
OLE_COLOR Properties


Properties of type OLE_COLOR store a
Long Integer color
User sees a color palette
Public Property Get ForeColor() As OLE_COLOR
ForeColor = object.ForeColor
End Property
Data type is
OLE_COLOR
Boolean Properties



Properties window displays a list box
List box contains True / False values
IntelliSense works too
Enumerated Properties

Like Boolean properties
• Properties window displays a list box
• List box contains enumerated values
• IntelliSense works too

Both intrinsic and user-defined
enumerated properties are supported
Intrinsic Enumerated
Properties


Use constants such as
MousePointerConstants
Values derived from type libraries - Use
Object Browser to locate
Public Property Get MousePointer() _
As MousePointerConstants
' Statements
End Property
Intrinsic Enumerated
constant
User Defined Enumerated
Properties


Define an Enumerated type
Create properties of that type
Public tbpEnum
tbpMaybe = 0
tbpYes = 1
tbpNo = 2
End Enum
Public Property Get Indecision() _
As tbpEnum
End Property
Property Pages Introduction

Adding Property Pages to an existing control
• Sample control and properties in this chapter are
more complex
• Chapter example implements a calendar to
display the days of the week and the
corresponding date
• Example – Ch12_C.vbg
UserControl
Hidden
constituent
controls
Exposed
properties
Properties
window
Property
Pages
Introduction to Property
Pages

Just another type of module
• Each Tab on the set of PropertyPages
corresponds to a Property Page module
Creating Property Pages


Add Property Page modules to a project
Set the PropertyPages property for an
ActiveX control to add the pages
Designing Property Pages









When displayed, first tab should receive focus
All fields should support an access key
Use a standard size
Use a label prompt having the same caption
as the property name
When a property stores an enumerated type,
use a ListBox or ComboBox for selection
Properties having a similar purpose should
appear on the same tab
Standard page tabs should appear last
Do not display dialogs
Avoid graphics. Keep the pages fast
Standard Property Pages




StandardFont allows the user to set
fonts
StandardColor allows the user to select
colors
StandardPicture allows the user to
select pictures
StandardDataFormat used with data
binding
Connecting Property Pages

From the Properties window for the
User control, select PropertyPages to
display this dialog
User-defined property
pages
Standard property
pages
StandardColor Property
Page
Automatically displays
properties of type
OLE_COLOR
User-Defined Property Page
Modules

Module type is PropertyPage
 Events
• SelectionChanged occurs when the user selects
different control instances
• ApplyChanges occurs when developer clicks OK
or Apply on PropertyPages
• Initialize event occurs when PropertyPage is
displayed

Properties
• Boolean Changed property indicates that a value
has changed (OK and Apply are enabled)
• SelectedControls collection contains a reference
to control instances selected by the developer
Creating Property Pages



Create constituent controls similar to a
User control
Must initialize constituent controls using
SelectedControls collection
Enumerated types must be managed
manually
• Load the valid enumerations at design time
• Explicitly set the selected item to the
current property value
SelectedControls Collection


Contains a reference to the controls
selected by the user
Zero-based
txtEndYear = SelectedControls(0).EndYear
First control instance
Applying Changes


Respond to the ApplyChanges event
Use SelectedControls collection to get a
reference to the control instance
Private Sub PropertyPage_ApplyChanges()
SelectedControls(0).StartYear = txtStartYear
SelectedControls(0).EndYear = txtEndYear
End Sub
First control instance
Just Do It!




Work on Exercise 12.1 from the
textbook (page 725)
There is an executable demo (JDI2.exe)
located in the Classroom Lecture
Demos – feel free to run that to get a
better idea of what you’re being asked
to do
We will discuss the solution after you’ve
worked on problem for a while
Call me over if you have questions –
don’t sit there stumped for long
What Next?

Next Week
• Read Chapter 16 – Understanding the
Windows Application Programming
Interface
• Programming the Windows Registry
• The Basics of Windows Dynamic Link Libraries
• No Homework – work on your Midterm
Project