Return
to IFS 110 class Notes Menu!
Project
2
Working
with Intrinsic Controls
and ActiveX Controls
Intrinsic
Controls
- Intrinsic
controls - are
the basic set of twenty controls in
the Toolbox.
- Controls exist within the Visual
Basic .exe file
- do not
have to add
these to Toolbox
- can not be removed
from the Toolbox
ActiveX
Controls
- ActiveX
controls -
exist as separate files
- in earlier versions of Visual
Basic called VBXs
(Visual Basic Extensions)
and later OCXs .ocx extensions
- include these controls by adding
them to Toolbox
- Microsoft includes many ActiveX
controls with the Professional and Enterprise editions of VB
- thousands of ActiveX controls are
available from software vendors
ActiveX
Controls
- Over 2000
ActiveX controls are available today.
In Addition version 5 is the first version
of Visual Basic that enables
you to create
your own controls.
You can create controls
from scratch, customize
controls, or combine controls
to make new controls.
Building a Project
3 Step
Process
- Create the Interface
- Set Properties
- Write Code
Form
Size and Position
- Resize form - during design-time by changing the
values of its Height and Width properties
- Position form - by changing value of its Top and
Left properties
- StartUpPosition
property - can
be set at design-time to center
the form on the desktop
- Code statements can be used at run-time to center the
form on the desktop
Labels
and AutoSize Property
- Note: contents of a label control
cannot be directly changed by the user
- AutoSize property of
a label to True, the labels size automatically is
adjusted to size of labels caption
- If Property has 2 values (False/True)
- Double-clicking the name of
property switches between values
Labels
and Backstyle Property
- BackStyle
properties
- 1 - Opaque - (default) BackColor
property displays within the labels borders
- 0 - Transparent - color of control
below the label (form) displays within the labels
borders
Copying Controls
- Note: Want to add multiple
similar controls to a form
- Copy control to Clipboard
- Paste copies from Clipboard to form
List
Box Control
- Note: used to present list
of choices
- List box - part or all of the list of choices
displays
- Scroll bar automatically added
if list too long for the size of the list box
- Drop-Down list - list of choices appears only when you
click
the down arrow located
next to the list box
Combo
Box Control
- Combo box controls
can have different selection methods
and
can appear differently,
depending on how properties set
- (Style 0) - Drop-down list
- Selection - click
in list or type
item in combo box
- (Style 1) - List always shows; scroll bar added
if list is longer than controls size
- Selection - click
in list or type
item in combo box
- (Style 2) - Drop-down list
- Selection - click
in list
Shape Control
- Shape control is used to add a rectangle,
square, ellipse,
or circle to a
form
- Note: change the shape
property of the Shape control in the Properties Window
to
get the different shapes
- Default shape of Shape Control is (0
- Rectangle) (1-3 other shapes)
Check Box Control
- Check box control is used in applications to
turn
options on or off
- Clicking
an empty check box places an check
mark in the box (option selected)
- Clicking
on a selected
check box removes
the check mark (option not selected)
- Note: When placed inside
a frame as many check boxes as
desired may be selected (not mutually
exclusive)
Frame Control
Used as a container for other controls
Differences from Shape Control
- Only rectangular
shape
- frame can have caption
- Only one option button
can be selected
at a time when inside
a frame (mutually exclusive)
Option Button Control
- Option buttons present
a set of mutually exclusive choices
(radio buttons)
- Note: All option buttons on a form function as one group unless
they are inside a frame.
- For an option button to be part
of a group, it must be added
directly inside frame
- Cannot add an
option button to form and drag it inside frame
Option Buttons continued
- Cut and Paste method or Double-click
method to add
option buttons can not be used since
they would have added option buttons to the form instead
of inside the frame
- Note: Double-clicking
on a Control button from the Toolbox
always adds the control to the center
of the form
TextBox
Controls
- At run-time,
the Microsoft Windows
operating system automatically implements a standard
keyboard interface to allow navigation in Textbox
controls. The user
can enter, edit,
and delete text
with the ARROW keys
(UP, LEFT, DOWN, and RIGHT; and also use the DELETE, BACKSPACE,
HOME,
and END keys)
- On some Visual Basic controls, a scroll
bar is automatically added when
contents extend beyond controls borders. This is not
true of text boxes, must use ScrollBars property
Common Dialog Control
- Common dialog control is
used in applications to display one of five
common dialog boxes at run-time
- Open, Save
As, Print, Color, and Font
- Common dialog control invisible
(run-time)
- Dialog box opened during run-time
- by applying one of the Show methods
to CommonDialog control through code
- ActiveX control
- must be added to Toolbox before you can add it to an
application
Common Dialog Control
- Note: when you choose OK from a color
dialog box after selecting
a color during
run time, the selected color becomes
the value of the
common dialog controls Color property
- Through a code
statement set BackColor
property of another control equal
to value of the common dialog controls Color
property
Color Dialog Box
Actions Users Perspective
- Color
was selected in
the dialog box
- the OK
button was chosen
- a controls
color then changed to the selected
color
Note: movie
theater attendant
will not be able to change
the Movie Box Office applications
color. Programmer
can use the Color dialog box to show
different color possibilities
to ANC management.
(double-click
on form to activate
- no directions
given to user for this)
Aligning
Controls
- Use VB6s Format menu or Form Editor Toolbar to access form
layout commands that adjust the alignment, spacing, and
size
of any group of controls
on a form
- Align command - aligns selected objects with
each other using
the last selected object
in a group
Setting Properties
Naming Controls
Note: As controls are added to
form Visual Basic assigns a name
to
control (Default Command1)
Note: Easier
to read and edit code if controls have names that
represent purpose
of control within application. (Better cmdOK)
Visual Basic Controls Prefixes (Table 2-2 VB 2.27)
Control
CheckBox
ComboBox
CommandButton
Data
DirList
Box
DriveListBox
FileListBox
Form
Frame
Grid
HS
scrollBar
Image
|
Prefix
chk
cbo
cmd
dat
dir
drv
fil
frm
fra
grd
hsb
img
|
Control
Label
Line
ListBox
Menu
OLE
OptionButton
PictureBox
Shape
TextBox
Timer
VScrollBar
|
Prefix
lbl
lin
lst
mnu
ole
opt
pic
shp
txt
tmr
vsb
|
Adding
Items to Drop-Down List
(2 Ways)
- Design-Time - add items to ComboBox
or ListBox
control by adding items to the controls List property
- use CTRL-Enter at end of each item
in list
- Run-Time - use AddItem method using
code
example: (put in Form_Load procedure)
- cboDropDownListBox.Additem "List
Item One"
- cboDropDownListBox.Additem "List
Item Two"
- cboDropDownListBox.Additem "List
Item Three"
Setting Label Properties
- BorderStyle Property - changes border look
- 0 - None
(default)
- 1 - Fixed
Single
- Note: double-click
on property to change
- FontSize Property - changes size of letters
- Note: FontSize value after current
value in list can be selected by double-clicking
Visible Property
- Determines if control can
be seen at run-time.
- Used in movie
application to make the 5th radio
button invisible
so that when application starts the user does not see any
number of tickets selected on the radio button frame.
- Code can be used to
control
property
Default Property
Command Buttons
- Default property of
a command button allows you to press ENTER key as an alternative
to clicking the
button
- change default property to True by double-clicking
- Note: Only one
command button on form can have its Default
property set to True.
- If a different button is chosen to
have this property, Visual Basic will change 1st button to False.
Writing Code
Assignment statements
Controlname.property
= value
Example:
cmdEnter.Default = True
To Save the Form (Steps)
- Click
the Save Project button
on Standard
toolbar
- Type Name of
Form in File Name box in
the Save File As dialog box
- Click Floppy drive
A in the Save in
list box
- Click
the button in the Save File As
dialog box
- Type Name of
Project in File Name box in
the Save Project As dialog box
- Click
the button in the Save Project As
dialog box
Movie Box Office
Application
- Seven events in
the movie box office application require event procedures
or subroutines.
- See MOVIE BOX
OFFICE EVENT SUMMARY TABLE 2-5 page VB 2.42
General Declarations
Subroutine
- Variables
- used in code statements to store temporary values
- data type -
determines what kind of data the
variable can store (numeric
or character etc.)
Rules Variable Names
- Begin with
a character
- Less than 255
characters long
- Cannot contain punctuation
or blank spaces
Variables Continued
- Easiest way to create a variable
is
to assign a value
to variables name in a code
statement
Example:
rate = 3.5 or name = "John"
- String -
character data enclosed in quotation marks
Code that Creates
Variables
EXAMPLE
TYPE
numeric
data
string
data
another
variable
expression
|
STATEMENT
price
= 1.35
movie
= "ET"
cost
= price
net
= grosspay - taxes
|
Implicit
and Explicit Variables
- Implicitly
declared variables
- variables created simply by using
them
- Explicitly
declared variables
- variables declared in a separate
code statement
Dim variablename
As datatype
- Data types - Byte, Boolean, Integer,
Long, Currency, Single, Double, Date, String,
Object, Variant,
a user-defined
type, or an object type
- Scope of
Variable -
refers to whether a variable
is available for
use by different subroutines
Variables
Continued
- Local Variables -
When variables are created simply by
using them, they can be used
only within the subroutine in which
they were created.
Lose their value between calls
unless they are declared static
- Global
Variables - Variables declared
in a special procedure (General Procedure) where both the
variable name
and data type must
be specified.
These variables can be shared
by many procedures.
Declaration Statement
Dim variablename As datatype
Example:
Dim num As Integer
Note: Case
and color
automatically changed
Note: VB inspects code for common
errors when ENTER
key pressed at end of line.
Editor
Options
- Visual Basic has several optional
code writing and editing features
such as automatically verifying
correct syntax
after you enter a line of code.
- You can enable
or disable these
features on the Editor tab sheet
in the Options dialog box,
accessed on the Visual Basic Tools
menu
Creating Procedures
Several Controls (Steps)
- Select control to
assign code from Object list box
- Select
desired event
from Procedures list box
- Enter code statements
Order Of Precedence
TABLE
2-7 VB 2.45
^ Exponents
* Multiplication
/ Division
returning floating-point
result
\ Division
returning integer
result
MOD Divide
two numbers returning remainder
+ Addition
- Subtraction,
negative
value of expression
(parenthesis
used to change order
of operations)
|
If... Then...Else Statement
Single - line
General
format:
If condition Then true-task Else false-task
Condition - is
made up of two expressions and a relational
operator.
Example: If chkMatinee.Value = 1 Then price
= 3.5 Else price = 5
RELATIONAL
OPERATORS
(Table
2-8 VB 2.46)
Comparison
Operator
=
<
>
<=
>=
<>
|
Meaning
is equal
to
is less
than
is greater
than
is less
than or equal to
is greater
than or equal to
is not
equal to
|
Format$ function
Format$ (expression, predefined
format name)
Example:
Format$ (grosspay, "currency")
Note: function
must be called in an expression can not be used by itself
Control.property =
Function (argument list)
Format$ function
lblAmtdue.Caption = Format$ (num * price, "currency")
- Currency
is a predefined format name (VB
will display in dollars
and cents)
Note: $ character appended to Format tells
VB to change numeric result of
expression to a string before it is assigned
to caption of label
- Predefined
formats (Table 2-9 VB 2.47)
(General, Currency, Fixed, Standard, Percent, Yes/No)
Option1_Click
Subroutine
(3 options)
- Num variable
assigned to number of tickets
- Determine
ticket price
Value property of
check box
is 1
if checked 0 if not
- Calculate
amount due (num*price)
display
it as dollars and cents
in Amount Due box
Coding Option1_click
Sub Option1_Click ( )
calculate and
display amount due
num = 1
If chkMatinee.Value = 1 Then price = 3.5 Else price
= 5
lblAmtdue.Caption = Format$(num * price, "currency")
End Sub
cmdEnter_Click
Subroutine
- Event adds
the number of tickets and movies
name to the top of a scrollable
list in the transaction record (txtRecord)
and clears the movie name, matinee
check box, number of tickets, and amount due.
String
Data Functions
- Concatenation -
adding strings together (ampersand
(&) character used)
name$ = title$
& " " & firstname$ & " "
& lastname$
- Chr$ function
- Chr$(10) - line
feed code
- Chr$(13) - carriage
return code
- Chr$(13) & Chr$(10) when used together causes a
new line to be
started
Constants
- Constant - similar to variable, reserves
space in memory, but remains
same during execution
of program.
- Const statement - used in General
declarations section to define
a constant
- Predefined
Constants - Visual Basic
contains a large set
of predefined constants
- vbNewLine - equivalent to the
characters chr$(10) & chr$(13)
Combo
List Box Principles
- Index - Visual Basic assigns consecutive
numbers to items
added to Combo
Box or List Box
- first item given
an index of 0
- ListIndex
property - item selected
by user during run-time given
the value of
that items index
- can select
an item from list using code
by changing the controls ListIndex
property
- ListIndex value of
-1 means no item selected
Code
cmdEnter_Click
Sub cmdEnter_Click ( )
Update
transaction list and clear settings
txtRecord.Text = num & " " & cboShow.Text
& vbNewLine & txtRecord.Text
cboShow.Listindex = -1 list box empties selects
blank list item
chkMatinee.Value = 0 uncheck matinee box
Option5.Value = True select invisible button
lblAmtdue.Caption = "" clear caption
End Sub
CommonDialog
Control Code
- Show methods - used to display
a common dialog
during run-time
using code with CommonDialog
control
- ShowColor
method
- used to display Color Dialog Box
- Flags property - used to define
the dialog boxs properties
and behaviors
using CommonDialog Control
- Color property of CommonDialog Control
is assigned to
the BackColor
property on controls
using code
Project
2
Working with Controls
END
Program
Creating Drop-Down Application Requirements
Return
to IFS 110 class Notes Menu!