Discussing Forms Again
In addition to form design principles, you also need to consider where your application begins and ends. There are a few techniques for deciding how your application will look when it starts. It's also important to be familiar with some of the processing that occurs when an application is uninstalled.
Set startup form
By default, the first form in the application is designated as the startup form. This form is displayed when the application starts running (so the first code to be executed is the code in the Form_Initialize event of the form). If you want to display another form when the application starts, you have to change the startup form.
To change the startup form, follow these steps:
1. From the Project menu, select Project Properties.
2. Select "General".
3. In the "Startup Object" ListBox, select the form to be the new startup form.
4. Select OK.
Start without startup form
Sometimes you may want to start the application without loading any forms. For example, you might want to run code that loads a data file and then decide which of several different forms to display based on the contents of the data file. To do this, create a subprocess named Main in the standard module, as shown in the following example.
SubMain()
DimintStatusAsInteger
'Call a function procedure to verify user status.
intStatus=GetUserStatus
'Display a startup form based on the status.
IfintStatus=1Then
frmMain.Show
Else
frmPassWord.Show
EndIf
This procedure must be a subprocedure and cannot be within a form module. To set the SubMain process as the startup object, select "Project Properties" from the "Project" menu, then select "General", and then select "SubMain" from the "Startup Object" box.
Show quick display on startup
If there is a long execution process at startup, such as loading a large amount of data from a database or loading some large bitmaps, you may want to give a quick display at startup. A quick display is a form that typically displays content such as the application name, copyright information, and a simple bitmap. The screen displayed when you start Visual Basic is a quick display.
To display a quick display, you need to use the SubMain process as the startup object and use the Show method to display the form:
PRivateSubMain()
'Show quick display.
frmSplash.Show
'Add startup process here.
...
'Show the main form and unload the quick display.
frmMain.Show
UnloadfrmSplash
EndSub
A quick display can attract the user's attention when some startup routine is executing, giving the illusion that the application is loading quickly. When these startup routines are complete, the first form can be loaded and displayed quickly unloaded.
For quick-display designs, keep it as simple as possible. If you use a large number of bitmaps or a large number of controls, the fast display itself will load slowly.
End application
Event-driven applications stop running when all forms have been closed and no code is executing. If there are still hidden forms when the last visible form is closed, the application appears to have ended (because there are no visible forms), but in fact the application continues to run until all hidden forms are closed. until. This occurs because any access to properties or controls of an unloaded form will cause that form to be loaded implicitly and silently.
The best way to avoid this type of problem when closing an application is to ensure that all forms are unloaded. If there is more than one form, you can use the Forms collection and the Unload statement. For example, a command button named cmdQuit can be used on the main form to exit the program. If the application has only one form, the Click event procedure can be simply:
PrivateSubcmdQuit_Click()
UnloadMe
EndSub
If your application uses multiple forms, you can unload these forms by placing code in the main form's Unload event procedure. You can use the Forms collection to ensure that all forms are found and closed. The following code uses the forms collection to unload all forms:
PrivateSubForm_Unload(CancelAsInteger)
Dimiasinteger
'Loop through the forms collection and unload each form.
Fori=Forms.Count-1to0Step-1
UnloadForms(i)
Next
EndSub
There are some situations where it is necessary to terminate the application regardless of the state of existing forms or objects. For this purpose, Visual Basic provides the End statement.
The End statement causes the application to end immediately: the code after the End statement will not be executed, and no further events will occur. In particular, Visual Basic will not execute any of the form's QueryUnload, Unload, or Terminate event procedures. Each reference to the object will be released, but if you define your own classes, Visual Basic will not execute the Terminate event of objects created by these classes.
In addition to the End statement, the Stop statement can pause an application. However, the Stop statement can only be used during debugging because it does not release the reference to the object.
For more information about the Stop statement, see "Using Interrupt Mode" in Chapter 13, "Debugging Code and Handling Errors," and "Stop Statement" in the Language Reference. For information about form collections or releasing object references, see Chapter 9, "Programming with Objects."
Using menus in applications
Many simple applications consist of only a form and a few controls, but the functionality of a Visual Basic application can be enhanced by adding menus. This section explains how to create and use menus in your application.
Create menus with the menu editor
Use the Menu Editor to create new menus and menu bars, add new commands to existing menus, replace existing menu commands with your own commands, and modify and delete existing menus and menu bars.
To display the menu editor:
From the Tools menu, choose Menu Editor.
-or-
Click the Menu Editor button on the Toolbar. This will open the menu editor, as shown in Figure 6.7.
Although most menu control properties can be set with the Menu Editor, all menu properties are also available in the Properties window. The two most important properties of the menu control are:
Name - This is the name used to refer to the menu control in code.
Caption - This is the text that appears on the control.
Other properties in the menu editor, including Index, Checked, and NegotiatePosition, are described later in this chapter.
Using the list box in the menu editor
The Menu Controls List box (located in the lower part of the Menu Editor) lists all menu controls for the current form. When you type a menu item in the title text box, the item also appears in the menu control list box. Select an existing menu control from the list box to edit the control's properties.
For example, Figure 6.7 shows the various menu controls for the File menu in a typical application. The position of a menu control in the menu control list box determines whether the control is a menu title, menu item, submenu title, or submenu item:
A menu control located flush to the left of a list box appears in the menu bar as the menu title.
An indented menu control in a list box will only appear on the menu when its leading menu title is clicked.
An indented menu control, if followed by some menu controls that are indented again, becomes the title of a submenu. Each menu control indented below the submenu title becomes the menu item of that submenu.
A menu control with a hyphen (-) as its Caption property appears as a separator bar. Separator bars divide menu items into logical groups. Note that a menu control cannot be used as a separator bar if it is a menu title, has submenu items, is checked or disabled, or has a shortcut key.
To create a menu control in the Menu Editor, follow these steps:
1.Select the form
2. From the Tools menu, choose Menu Editor. -or- Click the Menu Editor button on the Toolbar.
3. In the Title text box, type the text that you want to appear on the menu bar for the first menu title. If you want a certain character to become the access key for the menu item, you can also add an (&) character in front of the character. In menus, this character is automatically underlined.
The menu title text appears in the menu control list box.
4. In the Name text box, type a name that will be used to refer to the menu control in code. See "Menu Titles and Naming Guidelines" later in this chapter.
5. Click the left or right arrow button to change the indentation level of the control.
6. If necessary, you can also set other properties of the control. This work can be done in the menu editor or later in the "Properties" window.
7. Select "Next" to create another menu control. -or- Click Insert to add a menu control between existing controls. You can also click the up and down arrow buttons to move controls among existing menu controls.
8. If all the menu controls of the form have been created, select "OK" to close the menu editor. The created menu title will be displayed on the form. At design time, click a menu title to drop down its corresponding menu item.
separate menu items
A separator bar appears on a menu as a horizontal line between menu items. On a menu with many items, you can use separator bars to divide the items into logical groups. For example, Visual Basic's "Help" menu uses separator bars to divide its menu items into three groups, as shown in Figure 6.8.
To create a separator bar in the menu editor, follow these steps:
1. If you want to add a separator bar to an existing menu, select "Insert" and insert a menu control between the menu items you want to separate.
2. If necessary, click the right arrow button to indent the new menu item to the same level as the menu item it is separated from.
3. Type a hyphen (-) in the Title text box.
4.Set the "name" attribute.
5. Select OK to close the menu editor.
Note that although separator bars are created as menu controls, they do not respond to Click events and cannot be selected.
Assign access keys and shortcut keys
Improve keyboard access to menu commands by defining access keys and shortcut keys.
access key
Access keys allow pressing the ALT key and typing a specified character to open a menu. Once the menu is open, the control can be selected by pressing the assigned character (access key). For example, press the ALT E key to open the "Edit" menu, and then press the P key to select the "Paste" menu item. In the menu control's title, a specified access key appears as an underlined letter, as shown in Figure 6.9.
To assign access keys to menu controls in the Menu Editor, follow these steps:
1. Select the menu item to which the access key is to be assigned.
2. In its Title box, type an (&) character directly in front of the character you want to use as the access key.
For example, if the Edit menu shown in Figure 6.9 is open, the following Caption property settings value respond to the corresponding key.
Note that duplicate access keys cannot be used in menus. If multiple menu items use the same access key, the key will have no effect. For example, if C is the access key for both "Cut" and "Copy", then when you select the "Edit" menu and type C, the "Copy" command will be selected, but only after pressing the ENTER key, the application This command will be executed. And the "cut" command will not be executed at all.
shortcut key
A shortcut key will immediately run a menu item when pressed. You can assign a shortcut key to frequently used menu items, which provides a single-step keyboard access method, rather than the three-step method of holding down the ALT key, pressing the menu title to access the character, and then pressing the menu item to access the character. The assignment of shortcut keys includes a combination of function keys and control keys, such as the CTRL F1 key or the CTRL A key. They appear to the right of the corresponding menu item in the menu, as shown in Figure 6.10.
To assign a shortcut key to a menu item, follow these steps:
1. Open the "Menu Editor".
2. Select this menu item.
3. Select a function key or key combination in the "Shortcut Key" combo box.
To remove a shortcut key assignment, select "(none)" at the top of the list.
Note that the shortcut key will automatically appear on the menu; therefore, there is no need to type the CTRL key in the Title box of the menu editor.
Menu titles and naming guidelines
To maintain consistency with other applications, it's a good idea to follow established naming guidelines when creating menus.
Set Caption property
When assigning title values to menu items, you should try to follow the following guidelines:
1. The item names in the menu should be unique, but similar action items in different menus can have the same name.
2. The project name can be a word, a compound word, or multiple words.
3. Each project name should have a unique memory access character for selecting commands with the keyboard. The access character should be the first letter of the menu title, unless another character is easier to remember; two menu titles cannot use the same access character. For more information about assigning access keys and shortcut keys, see the "Creating Menus with the Menu Editor" section earlier in this chapter.
4. If a command requires additional information before completion, there should be an ellipsis (...) after its name, such as a command that displays a dialog box ("Save as...", "Preferences...").
5. Keep the project name as short as possible. If the application were to be localized, the word length would increase by nearly 30 percent in the foreign language version, which might not allow enough space to list the individual menu items. For more details on application localization, see Chapter 16, "International Distribution."
Menu naming convention
To make your code more readable and maintainable, it's a good idea to follow an established naming convention when setting the Name property in the menu editor. Most naming convention rules recommend identifying the object with a prefix (that is, mnu for a menu control), followed by the name of the top-level menu (such as File). For submenus, this is followed by the title of the submenu (such as mnuFileOpen).
For more information , see Appendix B, "Visual Basic Coding Conventions" for examples of recommended naming conventions. See "VisualBasic Coding Conventions" for examples of recommended naming conventions.
->