Set tab order
For example, suppose you create two TextBoxes named Text1 and Text2, and then create a CommandButton named Commandl. When the application starts, Text1 has focus. Pressing the TAB key will cause the focus to move between controls in the order in which they are created, as shown in Figure 3.20.
Setting the TabIndex property will change the tab order of a control. The control's TabIndex property determines its position in the tab order. According to the default rules, the TabIndex value of the first created control is 0, the TabIndex value of the second one is 1, and so on. When you change the tab order position of a control, Visual automatically renumbers the tab order positions of other controls to reflect insertions and deletions. For example, to make Command1 the first in the tab key sequence, the TabIndex values of other controls will be automatically adjusted upward, as shown in the following table.
Because numbering starts at 0, the maximum value of TabIndex is always one less than the number of controls in the tab order. Even if the TabIndex property value is higher than the number of controls, Visual Basic will convert this value to the number of controls minus one.
Note that controls that cannot gain focus, as well as invalid and invisible controls, do not have a TabIndex property and are therefore not included in the tab order. These controls will be skipped when the TAB key is pressed.
Remove controls in tab order
Normally, pressing the TAB key during runtime selects each control in the tab sequence. Set the control's TabStop property to False(0) to remove the control from the tab order.
A control whose TabStop property is set to False still maintains its position in the actual tab order, except that the control is skipped when the tab key is pressed.
Note that an OptionButton group has only one tab station. The TabStop property of the selected button (that is, the button whose Value is True) is automatically set to True, while the TabStop property of other buttons is False.
Menu basics
If an application provides a set of commands to the user, menus provide a convenient way to group commands and make them easily accessible to the user.
Figure 3.21 illustrates the menu interface elements of an untitled form.
The menu bar appears immediately below the form's title bar and contains one or more menu titles. When you click a menu title (such as "File"), a list containing the menu items is pulled down. Menu items can include commands (such as New and Exit), separators, and submenu titles. Each menu item that the user sees corresponds to a menu control defined in the Menu Editor (the Menu Editor is discussed later in this chapter).
To make your application simple and easy to use, menu items should be grouped by their functionality. For example, the file-related commands "New", "Open", and "Save As" in Figure 3.21 are all listed in the "File" menu. Some menu items perform actions directly, such as the "Exit" menu item in the "File" menu that will close the application. Other menu items display a dialog box, a window that asks the user to provide information the application needs to perform an action. These menu items should be followed by an ellipse (...). For example, when you select Save As... from the File menu, the File Save As dialog box appears.
A menu control is an object; like other objects, it has properties that define its appearance and behavior. Caption properties, Enabled and Visible properties, Checked properties, and other properties can be set at design or run time. The menu control contains only one event, the Click event, which is called when the menu control is selected with the mouse or keyboard.
For more information about the menu control, see "Creating Menus with the Menu Editor" in Chapter 6, "Creating the User Interface."
Pop-up menu
A pop-up menu is a floating menu displayed on the form and independent of the menu bar, as shown in Figure 3.22. The items displayed on a pop-up menu depend on the position of the pointer when the right mouse button is pressed. Therefore, a pop-up menu is also called a context menu (In Windows 95, clicking the right mouse button activates the context menu.) A pop-up menu should be used to provide a An efficient way to access common context commands. For example, right-click a TextBox and a context menu will appear (as shown in Figure 3.22).
Any menu with at least one menu item can be displayed as a pop-up menu at runtime. Use the PopupMenu method to display a pop-up menu.
For more information about pop-up menus, see "Creating Menus with the Menu Editor" in Chapter 6, "Creating the User Interface."
Use the menu editor
Using the "Menu Editor", you can add new commands to existing menus, replace existing menu commands with your own commands, create new menus and menu bars, and change and delete existing menus and menu bars. The main advantage of the Menu Editor is its ease of use. Menus can be customized in a fully interactive manner with minimal programming.
To display the Menu Editor,
Select "Menu Editor" in the "Tools" menu. This will open the "Menu Editor", as shown in Figure 3.23
Most menu control properties can be set using the Menu Editor. Likewise, all menu properties are available in the Properties window. Normally, you create menus in the Menu Editor, but to quickly change individual properties, use the Properties window.
For more information about creating menus and using the Menu Editor, see "Creating Menus with the Menu Editor" in Chapter 6, "Creating the User Interface."
Prompt the user with a dialog box
In Windows-based applications, dialog boxes are used to prompt the user for data required for the application to continue running or to display information to the user. A dialog box is a special type of form object that can be created in one of three ways:
Code using the MsgBox or InputBox functions can create predefined dialog boxes.
Create custom dialog boxes using standard forms or customizing existing dialog boxes.
Use the CommonDialog control to create standard dialog boxes such as Print and Open File.
Figure 3.24 is an example of using the MsgBox function to create a predefined dialog box.
When the MsgBox function is called in code, the dialog box is displayed. The code is as follows:
MsgBoxErrorencounteredwhiletryingtoopenfile,&vbCrLf&pleaseretry.,vbExclamation,TextEditor
You need to provide three pieces of information or three parameters to the MsgBox function: message text, a constant (numeric value) that determines the dialog box type, and a title. Creating dialog boxes is easier because styles can use a variety of combinations of buttons and icons.
Because most dialog boxes require user interaction, they are usually displayed as modal dialog boxes. Modal dialog boxes must be closed (hidden or unloaded) before continuing to use other parts of the application. For example, if you must click "OK" or "Cancel" before switching to other forms or other dialog boxes, then this dialog box is a modal dialog box.
A modeless dialog box does not need to be closed to allow focus to move between the dialog box and other forms. While the dialog box is displayed, you can continue working elsewhere in the current application. Modeless dialog boxes are rare; they are usually displayed because the application requires a response before continuing. The "Find" dialog box of the "Edit" menu in Visual Basic is a modeless dialog box. Use modeless dialog boxes to display frequently used commands or information.
More information For more information about creating dialog boxes, see Chapter 6, "Creating the User Interface."
->