CSCI 220 Computer Programming Visual Basic
Lecture 7: Menu design and the common dialog box
Menus:
How they act
Should follow Windows standards
Along top of screen
Alt + underlined letter for quick access
Shortcut keys
Can be accessed through arrow keys as well
How they are seen by VB
Attached to the form
Each menu item is a mini control with only a few
properties
Each menu item has only a CLICKED event it recognizes.
The menu design window
Reached through Window menu when appropriate form is
selected
Allows you to create outline of menu structure
Allows you to change properties of menu items
Caption: What the user will see in the menu
&Denotes NEXT letter will be underlined for quick
access
Name: like a control name, often begins with Mnu
Each menu item MUST have a name!!!!
Shortcut
Allows you to assign a Control combination or
function key to the menu item
Checked: Tells whether there is a check mark. TRUE
or FALSE
Enabled: Shows whether user can access the item
TRUE or FALSE
Visible: Shows whether item is visible. TRUE or
FALSE
Other properties rarely used
Accessing the code window for a menu item
Open up any code window in the current form, and
choose the menu item's NAME from the object list box
Access the window in DESIGN mode (program not
running) You will see the code window.
Common Menu guidelines
Usually 2-3 items deep, can be deeper
Should be less than 7-10 choices per menu
If possible, first letter should be underlined, be
related to shortcut key
Common Dialogs
Concept:
Dialog boxes already existing in Windows
Many commands are used in various types of programs
Save, Open, Choose Font, Choose Color, Font Size.
Dialog boxes related to these commands are available
as common dialogs.
Common Dialog Box Control works very differently from
most controls.
Control is NOT visible on the form.
Control Called by use of action statement
Example:
CmDialog1.Action = 3
Same control can "call" any of the dialogs depending
on the value of the action property.
Action Type of box
1 Open
2 Save
3 Color
4 Font
Properties
Filter
Used in file dialogs to determine how dialog will
isolate files
Format
*.TXT (Text Files)|*.TXT
No space on either side of |
InitDir
Used to set initial directory in file dialogs.
Color
Used to report back a color chosen from the color
dialog
File Handling
Purpose:
Method of handling external data.
Data is kept in FILES on the disk
Program can read,write information
Programmer responsible for controlling flow
Sequential Access files:
Work like cassette tape
Read from beginning to end
Can be slow with large number of records
Records can have variable lengths.
Useful when flexible structure is more important
than quick access.
The OPEN statement
Syntax:
OPEN filename FOR method AS # filenumber
Filename
Any valid DOS filename.
Be careful allowing the user to create file
names
Method
INPUT: reads information from an existing
file
APPEND: Adds information to end of existing
file
OUTPUT: Creates new file, adds info to it.
Use warnings
Usually output is used only when creating a
new file
It will destroy existing files of the same
name
APPEND is generally used for output
FileNumber
Must be an integer. Usually 1 or 2.
Keep it simple.
The Write # Statement
Used to write to a file
File must have been opened via OPEN
(Output/Append)
Syntax:
WRITE # FileNumber,Value(s)
FileNumber: a valid, open file
Value(s): Direct value, variables, separated by
commas
The INPUT # Statement
Used to get data from a file
File must have been opened via OPEN (Input)
Syntax:
INPUT # FileNumber,Value(s)
FileNumber: a valid, open file
Value(s): values, variables, separated by
commas
The CLOSE statement
Used to close a file, release its filenumber
Syntax:
CLOSE # FileNumber
The EOF function
Boolean (True/False) function
is True if the program has reached the end of the
file
Usually used in DO LOOPS with file handling.
Syntax:
EOF(FileNumber)
Example:
DO
...(program code)
LOOP UNTIL EOF(1)
Other new concepts:
NOT operator
Sets a boolean to its opposite value. Commonly used
for check boxes and boolean properties
Example:
Text1.FontUnderlined = NOT Text1.FontUnderlined
If FontUnderlined is true, it will become false, if
false it will become true
SETFOCUS method
Used in conjunction with an object. That object
receives the focus.
Example:
Text1.SetFocus
The Insertion cursor will appear in the text box,
any subsequent keypresses will be processed by text
box.
On Error Resume Next
Used when you anticipate an error in a procedure (esp.
file handling)
When an error occurs, program skips to next line
Good only in procedure defined
Your project: The text editor
You will create a simple text editor that will demonstrate
the use of menus and the common dialog box. Your menus
should work in the standard windows fashion, with underlined
characters and shortcut keys.
You will need a File menu with New, Open, save As, and Exit.
You will also need a Change menu containing Bold and Color
options. The Color option should call a submenu allowing
the user to choose foreground or background color. The main
part of the form should be taken up with a text box.
Create the Text box.
Make sure the multiline property is true
Set the scrollbars property to Vertical.
Add a common dialog box.
Set its Filter property to *.TXT (Text Files)|*.TXT
Set the InitDir property to A:\
Create a menu structure as described above. Give each menu
a meaningful name.
Pseudocode for menu items:
New
Set the Text property of the text box to ""
Use the SetFocus method on the text box
Open
Create a temporary string variable NextLine
Set the Txt property of the text box to ""
Call the OPEN common dialog
Open the FileName retrieved by the common dialog for
input (sequential access)
Do
Input from the file NextLine
Add NextLine to the text property of the text box
Until the end of the file
NOTE: Does the condition belong at the top or bottom of this loop?
Close the file
Use the SetFocus method on the text box
Save As
Call the SAVE AS common dialog
Open the file name retrieved by the common dialog for
output
Write the text property of the text box to the file
Close the file
Use the SetFocus method on the text box
Exit
End the program
Bold
Set the value of the menu's Checked property to the NOT
value of the property
(Reverse the property)
Set the value of the FontBold property of the text box
to be equal to the Checked property of the menu
Fore Color
Call the COLOR common dialog
Set the foreground color of the text box equal to the
color returned by the dialog.
Back Color
Call the COLOR common dialog
Set the background color of the text box equal to the
color returned by the dialog.
Test and save as VB7 HAVE FUN!!!
Return to Syllabus