Tutorial 6 1
List Box Control
Can be used to display a set of choices
from which the user can select only one
You also can create multi-selection list
boxes from which the user can select
zero or more choices (See Lesson A’s
Discovery Exercise 11)
Tutorial 6 2
GUI Standards for List Boxes
List boxes should contain a minimum of three selections
You should display a minimum of three selections and a maximum of eight selections at a time
Use a label control to provide keyboard access to the list box
Tutorial 6 3
AddItem Method
Used to add items to a list box
Syntax
control.AddItem item
item is the expression you want displayed, and it can be either numeric or string
if item is a literal string constant, then it must be enclosed in quotation marks
Tutorial 6 4
Arrangement of List Box Items
By use, with the most used entries
appearing first in the list
Sorted in ascending order either
alphabetically, numerically, or
chronologically
Tutorial 6 5
Sorted Property
Can be set to either the Boolean value True or the Boolean value False
If set the True, the list box items will be sorted in ascending order
If set to False, the list box items will appear in the order in which they were entered
Tutorial 6 6
ListIndex Property
You can use the ListIndex property to refer to an item in the list box
The first item in a list box has a ListIndex value of 0
If no items are selected in the list box, the ListIndex property has a value of -1
Tutorial 6 7
Default Item
It is customary to select a default item in a single-selection list box when the interface first appears
The default item should be either the most used selection or the first selection in the list
lstName.ListIndex = 0 will select the first item in the lstName list box
A default selection typically is not made in a multi-selection list box
Tutorial 6 8
Program Files vs Data Files
A program file contains the instructions
that both create the user interface and
tell the objects how to respond to events
A data file is a collection of information
composed of fields and records
Tutorial 6 9
Fields, Records, and Data Files
A field is a single item of information about a person, place, or thing
A record is a group of related fields that contain all of the necessary data about a specific person, place, or thing
A data file is a collection of related records
Tutorial 6 10
Sequential Access Data Files
Similar to a cassette tape in that each record in the file is both stored and retrieved in consecutive order
Advantage: easy to create
Disadvantage: you can process the records only in the order in which they were entered
Use for small files, files consisting only of text, or files that will always be accessed in sequential order
Tutorial 6 11
Open Statement
Open pathname For mode As # filenumber
pathname is the name of the file you want to open; it should include the drive letter and path, and it must be enclosed in quotation marks (unless it is stored in a String variable
mode can be either Input, Output, or Append
filenumber is a number that you assign to the file, and it must be an integer between 1 and 511, inclusive
Tutorial 6 12
Sequential Access File Modes
Input - you open a file for Input when you want to read the file
Output - you open a file for Output when you want to create a new file and then write data to it
Append - you open a file for Append when you want to add data to the end of an existing file
Tutorial 6 13
Record Pointer
When you open a file for Input, the record pointer is positioned at the beginning of the file, immediately before the first record
When you open a file for Output, the record pointer is positioned at the beginning of the empty file
When you open a file for Append, the record pointer is positioned immediately after the last record in the file
Tutorial 6 14
Write # Statement
Write # filenumber, [outputlist]
filenumber is the number used in the Open statement to open the file
outputlist is one or more numeric or string expressions, separated by commas
Tutorial 6 15
Close Statement
Close [# filenumber]
filenumber is the number used in the Open statement to open the file
A Close statement with no filenumber closes all open files
Tutorial 6 16
Verifying the Contents of a Sequential Access File
Use a basic word processor or text editor
Each line in the sequential access file represents a record
Fields in the record are separated by commas
String fields are enclosed in quotation marks
Tutorial 6 17
Sequential Access File
Tutorial 6 18
EOF Function
EOF(filenumber)
filenumber is the number used in the Open statement to open the file
EOF stands for “end of file”
returns the Boolean value True if the record pointer is at the end of the file (after the last record); otherwise it returns the Boolean value False
Do While Not EOF(1)
Do Until EOF(1)
Tutorial 6 19
Input # StatementInput # filenumber, variablelist
filenumber is the number used in the Open statement to open the file
variablelist is one or more numeric or string variables, separated by commas
Each variable in the variablelist is associated with a field in the record
The number, data type, and order of the variables in the variablelist must match the fields in the record
Tutorial 6 20
Print Method
Print [outputlist] - prints to the form
Printer.Print [outputlist] prints to the printer
Printer is a keyword that refers to the Printer object
Outputlist is the expression or list of expressions you want to print; if outputlist is omitted, the Print method prints a blank line
Tutorial 6 21
Print Method SeparatorsComma - tells Visual Basic to tab to the next print zone before printing the next character
Semicolon - tells Visual Basic to move to the next print position before printing the next character
You also can use the spacebar in place of the semicolon
Tutorial 6 22
Printing Strings and NumbersWhen you print a string literal constant, Visual Basic prints only the characters included within the quotation marks
Visual Basic prints positive numbers with both a leading and a trailing space
Visual Basic prints negative numbers with a leading minus sign and a trailing space
Tutorial 6 23
Spc and Tab Functions
Spc(number)
Number represents the number of spaces to insert before displaying or printing the next item in the outputlist
Tab(number)
Number represents the column number, or print position, to which you want Visual Basic to tab before printing the next item in the outputlist
Tutorial 6 24
Fixed-spaced vs Proportionally-spaced Fonts
Fixed-spaced fonts
Also called monospaced fonts
Use the same amount of space to print each character
Courier New is a fixed-spaced font
Proportionally-spaced fonts
Use varying amounts of space to print characters
MS Sans Serif is a proportionally-spaced font
Tutorial 6 25
Right-justifying and Aligning Numbers by Decimal Point
Use the Format function to ensure that
each number in the column has the
same number of digits to the right of
the decimal point
Use the RSet statement to right-align
the numbers within the column
Tutorial 6 26
Format Function
Format(expression, format)
expression specifies the number, date, time, or string whose appearance you want to format
format is either the name of a predefined Visual Basic format or a string containing special symbols that tell Visual Basic how you want the expression displayed
Tutorial 6 27
RSet Statement
RSet stringvariable = string
stringvariable is the name of a fixed-length String variable
A fixed-length String variable is one whose maximum length is defined when the variable is created
Dim variablename As String * number
string is the expression you want right-aligned
Tutorial 6 28
Printer Spacing ChartReports should have a report title and column headings
Detail lines show the information used to calculate the subtotals and grand total
999 indicates numbers having a maximum of 3 digits
XXX indicates a string having a maximum of 3 characters
Include an “End of report” message
Tutorial 6 29
MultiLine Property
Controls how many lines a text box can both accept and display
Can be set to either True or False (default)
Visual Basic automatically wraps the text in a multiline box if the text extends beyond the size of the box
Tutorial 6 30
ScrollBars Property
Specifies whether a text box has no scroll bars, horizontal scroll bars, vertical scroll bars, or both horizontal and vertical scroll bars
Visual Basic ignores the value in the ScrollBars property unless the MultiLine property is set to True
Tutorial 6 31
Sizing a Control Along with the Form
Set the control’s Height and Width properties to the form’s ScaleHeight and ScaleWidth properties
Enter the sizing code in the form’s Resize event, which occurs when the form is first displayed on the screen and when the user changes the size of the form either by minimizing, maximizing, or restoring it, or by using the form’s borders to resize it
Tutorial 6 32
Menus
Created in the Menu Editor
Can contain menu titles, menu items, separator bars, submenu titles, and submenu items
To avoid confusion, it is best to use one level of menus
Tutorial 6 33
Menu Elements in User Interface
Tutorial 6 34
Menu Elements in Menu Editor
Tutorial 6 35
Access Keys
Assign to each menu title and each menu item
Must be unique within a menu
Menu titles are accessed by pressing the Alt key and the access key
Menu items are accessed by pressing the access key when the menu is open
Tutorial 6 36
Shortcut Keys
Assign to commonly used menu commands
Displayed to the right of the menu item
Can be used only when the menu is closed
Tutorial 6 37
More on Menu Controls
You must provide a name and a caption for each menu control
Each menu title and each menu item is considered a separate control and it can have its own code
A menu control can recognize only the Click event
Tutorial 6 38
Separator Bar
A separator bar is a horizontal line used
to separate two groups of menu items
Created by entering a hyphen in the
menu control’s Caption property
Even separator bars must have a name
Tutorial 6 39
Clipboard Object
Provides access to the Windows
clipboard
Allows you to include cut, copy, and
paste capabilities in your application
Tutorial 6 40
Methods of the Clipboard Object
SetText methodsends text to the clipboard
Clipboard.SetText data
GetText() methodretrieves text from the clipboard
Clipboard.GetText()
Clear methodclears the contents of the clipboard
object.Clear
Tutorial 6 41
SelText Property
When a user selects text in a text box, Visual Basic records the text in the text box’s SelText property
This property contains a zero-length string if no text is selected
You also can use the SelLength property to determine if any text is selected in the text box
Tutorial 6 42
Clipboard.SetText “Have a nice day” Places the string “Have a niceday” on the clipboard
Clipboard.SetText txtEdit.Text Places a copy of the txtEditcontrol’s contents on theclipboard
Clipboard.SetText txtEdit.SelText Places a copy of the textcurrently selected in the txtEditcontrol on the clipboard
txtEdit.Text = Clipboard.GetText() Retrieves the text from theclipboard and places a copy of itin the txtEdit control, replacingthe existing text
txtEdit.SelText = Clipboard.GetText() Retrieves the text from theclipboard and places a copy of itin the txtEdit control, replacingonly the selected text
txtEdit.SelText = “” Removes the selected text fromthe txtEdit control
Clipboard.Clear Clears the contents of theclipboard
Tutorial 6 43
Edit Menu Click Event
Tutorial 6 44
Copy Command Click Event
Tutorial 6 45
Cut Command Click Event
Tutorial 6 46
Paste Command Click Event
Tutorial 6 47
String Manipulation Functions
Left(string, length)
returns the leftmost length characters in the string
Right(string, length)
returns the rightmost length characters in the string
Mid(string, start[, length])
returns length characters from string, beginning with start
Tutorial 6 48
Instr Function
Instr(start, string1, string2[,compare]start is a numeric expression that sets the starting position for the search
string1 is the string expression being searched
string2 is the string expression being sought
compare is either 0 (default; case-sensitive) or 1 (case-insensitive)
returns the starting position of string2 within string1
Tutorial 6 49
Debugging Technique
Visual Basic’s Edit menu provides two
commands that you can use to edit the
code in an application quickly. These
commands are Find and Replace