PRG 240, Advanced Visual Basic - Ch. 2 Notes
Objective #1: List the sequence of form events.
- Initialize - when an instance of the form is created; initialize
variables and properties here.
- Load - form is loaded into memory
- GotFocus - form becomes active
- Activate - form becomes the active window or becomes visible with
Show method
- LostFocus - another form becomes the active window
- Deactivate - another form becomes the active window
- QueryUnload - just before form is unloaded; can ask user to save
changes or cancel unload
- Unload - form is released from memory; release any other object variables
here too
- Terminate - all variables are set to Nothing
- Note that an End statement prevents QueryUnload, Unload, & Terminate
from occurring.
Objective #2: Describe the role of a collection and specifically the Forms
and Controls collections.
- A collection is a group of objects. Of course, you have already been
using objects (i.e. controls and forms) for some time.
- A collection's name is simply the plural form of the objects it contains.
Example - The Forms collection consists of form objects.
- A collection is an object with properties and methods itself.
- Its properties include Count.
- Its methods include Add and Remove.
- The Forms collection of a project consists of all loaded forms.
- The Controls collection of a project consists of all of the form's
controls.
Objective #3: Be able to create a MDI project with a parent and child
forms.
- Be able to distinguish between a multiple-document interface (MDI)
and a single-document interface (SDI). For example, when you have two
documents open within MS Word, you are using a MDI. The projects that you
wrote in CIS 230 were mostly, if not all, SDI's.
- There is one parent form in an MDI project. This form can include
child forms which appear within the parent's window.
- To add a parent form to an existing project, use the Project/Add MDI Form
menu command.
- Most of the time in a MDI project, all forms besides your parent form will
be child forms (except for possibly the splash screen). That is, the child
forms will appear within the parent MDI form. Set the MDIChild property to
True for all child forms.
- If a child form contains a menu, it will appear when the child form is displayed.
However, the parent's menu will appear if no visible child forms contain menus.
- It is a nice touch to create a Windows menu that allows the user to access
various child windows directly from the menu of the parent form. You can create
a Windows menu with the menu editor, just set the WindowList property to True.
- The Arrange method of a MDI form can be used to tile all open windows horizontally
(vbTileHorizontal), vertically (vbTileVertical), or in a cascade arrangement
(vbCascade.)
- The Forms collection contains all of the loaded forms of a project. You
should always unload all forms at the end of a project's execution. You can
use a For/Each loop with the Forms collection to do this.
Dim frmForm As Form
For Each frmForm In Forms
Unload frmForm
Next
In a MDI project, you can add this code to the Unload event of the parent
form so that all child forms that happen to be open will be unloaded when
the user chooses the parent's Exit command.
Objective #4: Use popup (shortcut) menus to enhance a VB project.
- You can easily create a popup (or shortcut) menu that displays when
the right-mouse button is clicked.
- Create the menu item (such as mnuPopup) at design time but make sure that
its Visible property is False.
- Create at least one menu command under the invisible menu item. (If desired,
other menu commands can be added with the Add method during run-time.)
- You can use a form's PopUpMenu method within the form's Form_MouseUp event
to determine when the right-mouse button has been clicked and you can reveal
the invisible menu at that time. Example:
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single,
Y As Single)
If (Button = vbRightButton) Then
PopupMenu mnuPopup
' or Me.PopupMenu mnuPopup
End If
End Sub
Objective #5: Use ImageList, Toolbar, StatusBar, TreeView, ListView, and
Tabbed Dialog controls.
- The ActiveX controls, ImageLists (ils), Toolbars (tlb), StatusBars
(sta), TreeViews (tvw), and ListViews (lvw), can be used if
you first use the Project/Components menu command to add the Microsoft Windows
Common Controls 6.0 to your project.
- ImageList
- An ImageList control is used to store graphics and icons that you wish
to use in a project.
- The prefix for an ImageList control is ils.
- You need to right-click on the ImageList control and then select Properties
in order to access its Property Pages.
- Each picture in an ImageList control is identified by its Index and/or
Key properties. Using the Key property is safer because Index property
values can change when other pictures are deleted from within an ImageList
control.
- See the Program Files/Microsoft Visual Studio/Common/Graphics folder
for pictures that you can add to an ImageList. If you have MS Office installed
on your PC, you may be able to use pictures from the Program Files/MS
Office/Clipart folder as well.
- Each ImageList control can only hold pictures of one size. Therefore,
if you wish to use 16x16 as well as 32x32 icons on a form, you will need
to ImageList controls on that form.
- Toolbar
- Toolbars are located across the top of a form.
- A Toolbar consists of one or more buttons. Each button can have a picture
and/or a caption.
- The proper prefix for Toolbar objects is tlb.
- In order to have graphics appear on a toolbar, you must have those graphics
included with an ImageList control and then designate that ImageList from
within the toolbar's Property Page.
- You can insert buttons on the toolbar at design time through its Property
Pages. Images from the designated ImageList can be chosen for each button.
- Each button can have a Key property which can then be used with a Select
Case statement in the Toolbar's ButtonClick event.
- A Toolbar button can be added at runtime with the Buttons collection's
Add method:
ToolbarName.Buttons.Add [Index], [Key], [Caption], [Style], [Image]
- StatusBar
- The proper prefix for StatusBar objects is sta.
- A StatusBar is located at the bottom of a form.
- A StatusBar consists of up to 16 Panels.
- Each panel can have a bit of text and/or a picture. Use the StatusBar's
Property Pages to set such properties.
- There are a number of styles of status bar panels that you can create
using VB's Style Property Constant.
staExample.Style = sbrDate
staExample.Style = sbrTime
staExample.Style = sbrNum ' Num Lock key
- A StatusBar panel can be added at runtime with the Panels collection's
Add method:
StatusBarName.Panels.Add [Index], [Key], [Text], [Style], [Picture]
- TreeViews
- The proper prefix for a TreeView control is tre.
- A tree view is what you see when you open Windows Explorer.
- A TreeView consists of nodes. The topmost entries are the root nodes.
All subentries are child nodes. Child nodes are only viewed when a root
node is expanded. The proper prefix to use with a node object is nod.
- Each node has a Text property which is the piece of text that can be
viewed on the screen.
- A TreeView's Nodes collection consists of the nodes in the tree view.
The Nodes collection has a Count property and Add and Remove methods.
- You can assign two different images to each node of a tree view. One
image is displayed when the node is expanded and the other can be displayed
when it is "closed".
- The images for a TreeView must be stored in an ImageList control as
specified in the TreeView's Property Pages.
- Each node has a Sorted property which can be set to True to automatically
sort that node's subentries.. But, if nodes are added during runtime,
the Sorted property must be set to true in an executable statement in
order for the child nodes to be sorted with the new node.
- The Add method of a TreeView's Nodes collection is used to add a node
during runtime:
Set nodNewNode = tvwExample.Add([Relative], [Relationship], [Key],
Text, [Image], [SelectedImage]
Relative is key value for any relative node while Relationship is usually
one of the following constants: tvwChild, tvPrevious, tvwFirst, tvwLast,
tvwNext.
- ListViews
- The proper prefix for a ListView control is lvw.
- A listview is what you see when you double-click the My Computer icon
and view folder's contents in one window.
- A ListView can be viewed in one of four different views: lvwIcon, lvwSmallIcon,
lvwList, and lvwReport.
- Two separate ImageLists are usually used in conjunction with a ListView.
One contains large icons for when the ListView is viewed in the lvwIcon
view and the other is for the lvwSmallIcon view.
- A ListView consists of ListItems which form the ListItems collection.
The proper prefix for a list item is itm.
- You can use the Add method of the ListItems collection to add list items
to the ListView during runtime.
Set itmNewNode = lvwExample.Add([Index], [Key], [Text], [Icon], [SmallIcon])
- A list view item can have subitems that appear in the report (lvwReport)
view similarly to the columns you see in the Details view of a folder.
- After you add an item, you can add its subitems with the ListSubItems
collection's Add Method during runtime:
lvwExample.ListItems(index).ListSubItems.Add [Index], [Key], Text
- Tabbed Dialog
- The standard prefix for a Tabbed Dialog control is sstab.
- A Tabbed Dialog control consists of three tab pages which are containers
like a Frame object.
- To place a control such as a textbox on a page of a Tabbed Dialog control,
you must single-click the textbox control's icon in the Toolbox and click
and drag within the tab page to place the control there. If you double-click
the control's icon in the Toolbox, the control will be attached to the
form and not the Tabbed Dialog control.
- The Click event of a tab control can be used to determine which tab
page the user is viewing.
- The Tabbed Dialog control (prefix sstab) is used by adding the
Microsoft Tabbed Dialog Control 6.0 ActiveX control.
Objective #6: Use error handling to protect your program to some degree.
- Since run-time errors generate unique error numbers, you can catch this
error number and attempt to handle the error without having the project end
prematurely. The On Error statement is used along with the Err
object to trap such errors. This technique is called error-trapping.
- If you wish to use error-trapping, you should use an On Error statement
at the beginning of any procedure where errors could occur.
- Add the statement
On Error GoTo HandleErrors
to the beginning of a procedure so that you can gracefully guard against possible
run-time errors within that procedure. HandleErrors is simply a programmer
identifier that is used as a line label somewhere below the On Error statement
as in
HandleErrors:
- The Resume Next statement can be placed in the HandleErrors section
in order for the program to continue with the next line after the one where
the error occurred. Just using a plain Resume statement (without the keyword
Next following it), execution will continue with the same line that caused
the error. This can be useful in some cases.
- Be sure to use the Exit Sub statement just above your HandleErrors:
line label in order to avoid having the HandleErrors section being executed
twice.
- If you wish to turn off error-trapping for some reason, use the On
Error GoTo 0 statement.
- The Err object can be used to determine what run-time error occurred since
its Number property stores the error number. The Err object is automatically
included with a project. For example if Err.Number = 11 then a division by
zero occurred. If Err.Number = 53 then a specified file was not found. If
Err.Number = 360 then an object was already loaded. You can also access the
Err object's Description property to see or recognize errors by description.
- Do not worry about the Raise method of the Err object for CIS 232.
Copyright © 2001 Curtis Minich. All Rights
Reserved.