Return to IFS 110 class Notes Menu!
Project
2
Working
with
Intrinsic Controls
and ActiveX Controls
Intrinsic Controls
Building a Project
3 Step
Process
- Create the Interface
- Set Properties
- Write Code
Form Size and Position
Labels and AutoSize Property
Note: contents of a label control cannot be directly changed by the user
Labels and Backstyle Property
Copying Controls
Note: Want to add multiple similar controls to a form
Note: used to present list of choices
Note: change the shape property of the Shape control in the Properties Window to get the different shapes
Note: When placed inside a frame as many check boxes as desired may be selected (not mutually exclusive)
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)
Note: All option buttons on a form function as one group unless they are inside a frame.
Note: Double-clicking on a Control button from the Toolbox always adds the control to the center of the form
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
Color Dialog Box
Actions Users Perspective
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.
- Color was selected in the dialog box
- the OK button was chosen
- a controls color then changed to the selected color
(double-click on form to activate - no directions given to user for this)
Aligning Controls
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)
CheckBox ComboBox CommandButton Data DirList Box DriveListBox FileListBox Form Frame Grid HScrollBar Image |
chk cbo cmd dat dir drv fil frm fra grd hsb img |
Control LabelLine ListBox Menu OLE OptionButton PictureBox Shape TextBox Timer VScrollBar |
Prefix lbllin 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
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
General format:
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.)
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
|
|
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 (Private) 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 (Public) 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.
General Format:
Note: Case and color automatically changed
Note: VB inspects code for common errors when ENTER key pressed at end of line.
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
|
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)
|
|
= |
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 |
General Format:
Format$ (expression, predefined format name)
Format$ (grosspay, "currency")
Note: function must be called in an expression can not be used by itself
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 notCalculate 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$(10) - line feed code
Chr$(13) - carriage return code
Chr$(13) & Chr$(10) when used together causes a new line to be started
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
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!