Return to IFS 110
class Notes Menu!
Project 7
Creating ActiveX Controls and
Distributing Applications
Introduction
- In previous projects, CommonDialog
control and DataBound ComboBox
control ActiveX controls used
- Hundreds of these
controls available from Microsoft
and other venders
- VB6 allows creation
of ActiveX controls which can be complied
as (.ocx )files for future use
in any application (control creation capability was
first present in version 5 of Visual Basic)
Introduction
- In this project will create ActiveX
StopWatch control
- In creating control will learn about Timer
control and Resize event and how to create
your own properties
for controls
- Incorporate StopWatch control in the Multiplication
Quiz application
- Learn about random numbers,
and several new properties of
Command buttons
Introduction
- Compile Multiplication
Quiz application into an .EXE file
- Use Visual Basic Package and
Deployment Wizard to create an installation
program for distributing the
application on floppy disks
- Use ActiveX Document
Migration Wizard to create a version of
Multiplication Quiz application that can be hosted
by Microsoft Internet Explorer
Multiplication Quiz
- Five multiplication problems
generated randomly and the timer
begins
- Color of answer boxes changes
to indicate correct and incorrect
answers
- Start and Stop
buttons contain graphic images and
display ToolTip text
Creating ActiveX Controls
- ActiveX controls - are standardized user interface elements
you assemble to create an application
- Temporally add an additional
project to the desktop to test
the design and run
time behavior of your control
- Compile control into an .ocx
file for use in other projects
3 Ways to Create Controls
- Create control from scratch
- Modify a single
existing control
- Assemble a new
control from several existing controls
In this project, assemble a
StopWatch control from existing Label, CommandButtons, and
Timer
controls
Creating a User Control
Interface
- An ActiveX control created with VB is
always composed of UserControl object,
plus any controls placed on the UserControl, called constituent
controls
- UserControl objects have code modules
and visual designers like forms
- Add constituent controls to UserControl
objects Designer window, same way controls are
added to forms
StopWatch Control
- StWatch control consists of five constituent
controls - three CommandButtons
controls, one Label control, and one
Timer control
see (Figure 7- 13 VB 7.11)
- Timer control - is visible during design time
but is not
visible at run time
Timer Control
- Timer control used to update
the display of elapsed time
- Timer control has only one
event - the Timer event
- Timer event occurs when a preset interval for a Timer control
has elapsed
- intervals frequency stored in the
controls Interval
property, specifies
length of time in milliseconds
Timer Control
- Interval property can set
number of milliseconds between 1 and 65,535 between calls
- Enabled property -
determines whether or not the control responds
to the passage of time
- set Enabled to False to turn a Timer control off
and to True to turn it on
Timer Control
- When Timer control enabled, its countdown
always starts from the value
of its Interval property setting.
When it reaches the end
of the countdown, it executes
whatever code you have added to the Timer
event procedure
- In StWatch control,
will start and stop Timer by changing value
of Enabled property through code
statements in CommandButton Click events
Writing UserControl Event
Procedures
- TotalSeconds variable -
used in StWatch control to keep
track of elapsed seconds from Timer
start to Timer stop
- declared in general
declaration section to extend its scope
to module level (used in Timer1_Timer and Command3_Click events)
Resize Event
- Resize event - occurs
when a form or a UserControl
first is displayed and when the size
changes
- When StWatch control added
to form or borders dragged, its constituent
controls should adjust
their sizes and positions
automatically
- each of controls is a different size,
but constituent controls always have
the same relative size and position
(table 7-3
VB 7.15)
Timer Operations
- Timer1_Timer event will execute every one second when its
Enabled property is True
- The integer number minutes
of elapsed time is calculated using the integer division
operator ( \ )
Minutes =
Format ( TotalSeconds \ 60, "00")
- The Mod operator
is used to divide two numbers and return
only the remainder
Seconds =
Format ( TotalSeconds Mod 60, "00")
Writing UserControl Property
Procedures
- Every ActiveX control
has a default set of properties
and values that display in Properties
list
- Also have access
to all properties of its constituent
controls
- When ActiveX control
is added to a project,
can access only the properties of UserControl. No longer can
access
the properties of the constituent controls
directly
Writing UserControl Property
Procedures
- The way to access
the internal behavior of the
UserControls constituent controls
is to create new properties at the UserControl
level and then use changes
in those properties to activate changes
in properties of constituent controls
- Write code to accomplish
this in Property procedures
Property Procedures
- Property procedures allow
you to execute code
when a property value is set
or retrieved
- can use to create
own properties
- Typical property made up
of pair of Property procedures
- Property Get procedure - retrieves
value of a property
- Property Let procedure - assigns
a value to a property
StWatch Control
(2 additional properties)
1. Buttons
property - gives the
user a choice of whether or not the 3 command
buttons in StWatch are visible
- value of Buttons property
is Boolean (True/False)
- these values map directly
to the True/False values of the Visible
property of the three command
buttons
StWatch1.Buttons =
False (will change Visible
property of 3 constituent CommandButtons
to False)
StWatch Control
(2 additional properties continued)
- If a user decides to make Buttons
property False, then a way
is required to start, stop,
and reset StWatch control through
code.
2. Action
property - values of 1(Start),
2(Stop), or 3(Reset) will change the Value property of
the corresponding CommandButton control to True using
code
Properties or Methods ?
- Action property seems
more like a method than a property
- In Project 2 used ShowColor
method of CommonDialog
control but action property could
also have been used
CommonDialog1.ShowColor
CommonDialog1.Action =
3
- earlier versions of VB Show
methods did not exist
- Implementation as a property
or method is sometimes ambiguous
and even arbitrary
WriteProperties Event
- WriteProperties event of UserControl
occurs when an instance of an object
is to be saved
- event signals to object
that state of object needs to be saved,
so it can be restored later
example - Borderstyle property
of label set to 1-Fixed Single, when application closed
and reopened property maintained
WriteProperties Method
( 3 arguments)
- String expression
- represents data value to be placed in property bag
- Data value to
save in property bag
- Default value
for data
Call PropBag.WriteProperty
("Caption",Label1.Caption,"Label 1")
WriteProperty method of PropertyBag
object used to write a value
of property
ReadProperties Event
- ReadProperties event -
occurs when loading an old instance
of an object that has a saved state
- ReadProperty method of
the PropertyBag object is used to read a value
for a property
- A PropertyBag object
is passed into an object through the ReadProperties event
ReadProperties Method
(2 arguments)
- String expression
representing a data value in the
property bag
- Value to be returned
if no value is present
in the property bag
Label1.Caption = PropBag.ReadProperty
("Caption", "Label 1")
PropertyChanged Method
- PropertyChanged method notifies
the objects container (form)
that a propertys value has
been changed. By notifying the
container that a propertys value has changed, the
container can update the Property window
with the new values of the objects properties. This
method notifies the container to activate the WriteProperties event to store the new value
of the property.
- Properties available only at run
time (Action property)
do not need to call the PropertyChanged method
Saving ActiveX Project
- UserControls stored in plain
text files containing source code
and property values of UserControl
and its constituent controls
- VB uses .ctl extension for source
files
- If UserControl or its constituent
controls use graphical elements,
which cannot be stored as plain text, such as bitmaps,
VB stores those elements in a .ctx file with same name as .ctl file (similar to
.frx files to store graphical elements of forms)
- ActiveX project stored in
project file (.vbp)
Adding Multiple Projects
- Version 5 was the first
version of Visual Basic that allowed you to have multiple projects
open in the Visual Basic
IDE at the same time.
- Allows you to open a new
Standard EXE project for testing
your ActiveX control, while your ActiveX
project is still open
- Can test and debug
the StWatch control before
it is compiled as an .ocx file
Compiling ActiveX Control
- Can add a .ctl file
to Standard EXE project and control
will be added to Toolbox
- .ctl file can be used
in project, if project it made into an .exe
file it becomes part of the .exe
file
2 Disadvantages to
Above Method:
- Newer version
of control forces the project to be rebuilt
- Every project
using control would need the .ctl file in its .exe
file
Solution - .ocx control available
to all applications easy to replace
with updated version
Multiplication Quiz
Application
(Setting Properties CommandButton)
- Style property - set to 1- Graphical
- Picture property can be
used to set a graphic
(bitmap,
icon, metafile,
GIF, JPEG) to be displayed in
command button
- ToolTipText property used
to set text that appears as a ToolTip
for the control
- ToolTip - is a word or short
phrase that describes the function of a toolbar
button or other button or tool
Writing Code
3 event procedures see (Table 7-8 VB 7.37)
- Form_Resize
- used to keep controls centered
within applications window when user
resizes it
- cmdStart_Click - resets answer text
boxes BackColor property; generates
5 problems; reset StWatch control
- cmdStop_Click - must stop StWatch
control and grade the answers
Form_Resize Event
(Respond to 3
different states)
- User
minimizes form - do nothing
- Not minimized and user drags width
less than original - return to original width
- User
makes width greater than
original (drags or maximizes)
- reposition controls
Form_Resize Code
If frmMultQuiz.WindowState = 1 Then
do nothing
ElseIf frmMultQuiz.Width < 6780 Then
frmMultQuiz.Width = 6780 user made form too small
Else user made form bigger than original
Frame1.Left = (frmMultQuiz.ScaleWidth -
_ Frame1.Width - 345 - 975) / 2
cmdStart.Left = Frame1.Left + Frame1.Width
+ 345
cmdStop.Left = Frame1.Left + Frame1.Width
+ 345
StWatch1.Left = Frame1.Left + Frame1.Width
+ 345
End If
Random Number Functions
- Random number functions -
used in applications to model events
whose outcome is uncertain
- Quiz - generates 5
sets of multipliers and multiplicands
(integers
between 0 and 9)
- Rnd function - returns a
value between 0 and 1 (0 < Rnd < 1)
example: .235431
- A large set of random numbers
would have a uniform distribution,
they are all equally likely to occur
Random Numbers Terms
Int ((upperbound -
lowerbound + 1) * Rnd + lowerbound)
- Randomize statement -
used to set the Seed equal to a function of the system
clock to produce a different Seed each time
you generate random numbers (else same sequence
of random numbers generated each
time)
- Seed value - starting
value used by Rnd
cmdStop_Click Event
- StWatch control stopped
by setting Action property equal to 2
- Although pairs of multipliers generated
randomly, answer can be checked
by comparing the value
entered in answer text box to the
product of the two numbers
- If these two match, BackColor property set to
green, and red
otherwise
Distributing Applications
- An .exe file may not
run on other PCs,
because an .exe file requires additional files
to be able to run if VB not installed
- At a minimum, an
application requires the MSVBVM60.dll
file, Visual Basics run time dll
- Other files that might be
needed include ActiveX controls, data files,
and dynamic
link libraries (.dll)
Distributing Applications
- Cannot distribute application
by copying files to floppy
disk (reasons)
- Requires more than one
floppy - files must be transferred to hard
drive
- Need some way to tell user what
folders to copy files to
- If user copies .dll
or .ocx file over a newer version
used by another application, VB program will work but
other application wont
- User will have to add
program group and include it on the Start
menu
Distributing Applications
Solution
- Users expect to be able to install
software by using user-friendly
setup program
- Package and
Deployment Wizard - uses
a two step process to build setup application
and distribution disks
- In the Package
step it determines which files
are needed and where
they need to be installed on users
machine. It bundles these files into one or more
compressed cabinet (.cab) files. It creates a setup
program for the user.
- In the Deploy step
it writes all necessary files to distribution
media selected (floppy disk, network server,
Internet)
Packaging a Setup Program
- Package and Deployment Wizard
- separate application run from
Visual Basic group
- Package and Deployment Wizard
- also available as an add-in when loaded using the Add-In
Manager dialog box. A project must be saved and compiled
before it can be packaged and deployed.
- additional advanced
features and options
Installing, Running,
Applications
- When setup program run, decompresses files,
copies
them to appropriate folders after
checking for newer versions, enters them into the system registry, creates program
group and Start links
- It prompts user
for installation information suggesting default
values (folder where program files will be copied)
Removing
an Application
- When user runs setup program,
an application removal utility is
copied to users Windows folder
- also creates removal log
file (which directories were created
and which files were created where,
any changes to system registry, and program
groups, icons, and Start
menu created)
- Removal utility and log file
- allow user to uninstall
application through Add/Remove
Programs utility in Windows Control panel
ActiveX Documents
- ActiveX containers -
application used to view a document that did not create
it
- (Internet Explorer viewing Word document)
- When you open a Word
document in a container
such as Internet Explorer, the Word
application becomes a server that supplies
the objects that enable
the ActiveX container to view
and activate the document
- Same mechanism
works for ActiveX documents created
with Visual Basic
ActiveX Projects in VB
- When ActiveX document project
created, it can be contained in an ActiveX
container
- Compiling ActiveX document creates both a Visual Basic
document
file (.vbd extension)
and its server, which can be an ActiveX
.exe file
- .vbd file is to the .doc
file what the ActiveX .exe
file is to the Winword.exe file
- Can create ActiveX documents
from scratch or can convert
existing Standard EXE projects
Internet Distribution
- If you have a Web site,
you can link to your ActiveX
documents from any Web page
- Accomplished with a combination of HTML
and VBScript
- Can create a link to ActiveX
document that causes the browser
to find and download automatically all components
needed to run your ActiveX
document
Converting
Projects to ActiveX Documents
- ActiveX Document Migration
- process of converting Standard
EXE VB projects into ActiveX
documents
- ActiveX Document
Migration Wizard - VB add-in
that facilitates conversion
- converts a form
into ActiveX document object (file
with .dob extension)
- when existing project made an ActiveX
server by making it an ActiveX.EXE
file, VB also converts the document object into an ActiveX
Document (.vbd ext)
Startup Object Modifications
- In order to make an ActiveX.EXE
file from an existing Standard EXE
project, modifications
need to be made to projects Startup
object
- There only can be one Sub
Main in an application and must be created
within a code module rather than a
form
- Code module exists as a separate
file which can be added
and removed from applications like a
form
- Startup object of ActiveX. EXE
project must be Sub Main, even if
the Sub Main procedure does not contain any code
Project
7
Creating ActiveX Controls and
Distributing Applications
END
Use the
Icon on the right to view the steps to convert an ActiveX
document file to a HTML file using the Package and
Deployment Wizard . This completes the process at the end
of chapter 7 in your textbook on pages VB 7.63.
Creating and
Using ActiveX Control Requirements
Return to IFS 110
class Notes Menu!