Return
to IFS 110 class Notes Menu!
Project 7
Creating ActiveX Controls and
Distributing Applications
Introduction
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
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
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
Timer Control
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
Writing UserControl
Property Procedures
Property Procedures
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 ?
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
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 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
Function
- Random number
function -
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)
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
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 document
from scratch or can convert
existing Standard EXE projects
Internet Distribution
- If you have a Web site,
you can link to your ActiveX document
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 document
- 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!