The Aqivate and Deactivate Events VB Help

When more than one Form is displayed, the user can switch from one t~·the other with the mouse or by pressing Alt+Tab. Each time a Form is activated, th.e Activate event takes place. The Forms application uses the Activate event to set the Form’s I caption to a message rdicciting that this is the current Form:

Private Sub Form_ActivateO “,
Form2.Caption= ‘Form2 Activated’
End Sub

Likewise, when a Form is activated, the previously active Form receives the Deactivate event, which the application uses to change the Form’s caption:

Private Sub Form_Deact;vate()
Form2.Caption – ‘Form2 Inactive’
End Sub

Designing Menus

Menus are one of the most common and characteristic elements of the Windows user interface. Even in the old days of character-based displays, menus were used to display methodically organized choices and guide the user through an application. Despite the visually rich-interfaces of Windows applications and the many alternatives, menus are still the most popular means of organizing a large number of options. Many applications duplicate some or all of their menus in the form of icons on a toolbar, but the menu is a standard fixture of a Form. You can turn the toolbars on and off, but not the menus.

The Menu Editor

Menu can be attached only to Forms, and you design them with the Menu Editor. To see how the Menu Editor works, start a new Standard EXE project, and when Forml appears in the design window, choose Tools >Menu Editor to open the Menu Editor, as shown in Figure 4.7. Alternatively, you can click the Menu Editor button on the toolbar .

In the Menu Editor window you can specify the structure of your menu by adding one command at a time. Each menu command has two mandatory properties:
• Caption.This is the string that appears on the application’s menu bar.
• Name This is the name of the menu command. This property doesn’t appear on the screen, but your code uses it to program the menu command.

The Caption Name properties of a menu item are analogous to the propertiesthat have the same natne as the Command button or Label con trol. Caption is what the user sees on the Form, and Name is the means of accessing the control from within the code. As far as your code is concerned, each menu command is a separate object, just like a Command button or Label control.
To add commands to the Form’s menu bar, enter a caption and a name for each comm~d. As soon as you start typing the command’s caption, it also appears in a new line in the list at the bottom of the Menu Editor window. Let’s create the menu structure shown in Figure 4.7. This menu contains two commands, File and Edit. When the user clicks on either one, the submenus are displayed.

The commands that belong to each menu form the corresponding submenu and ale indented from the left. To design the menu, follow these steps:

1. Open a new Foriu in the Design pane, and choose IOcI~ Me~u Ijditor to open the Menu Editor window.
2. In t.he Caption box, type the caption o.f,the first command (File).
3. In the Name box, enter the command’s name (FileMenu).
4. Press Enter or click Next to enter the next command. Repeat steps 1 through
3 for all the commands listed.

If you run the application now, all the commands you’ve entered will be displayed along the Form’s menu bar. If the window. isn’t wide enough to fit the entire menu, some of the menu’s commands will be wrapped to a Second line. To create the menu hierarchy (make the commands appear under the File and Edit headings), you must indent them. Select the Open command in the list and click the button that has the right-pointing arrow. Do the, same for the Save, Copy, Cut, and Paste commands. Your Menu Editor window should look like the one. If you run the application now, the menu has the proper appear~ce. Each subordinate command appears under the first-level menu command to which it belongs. To view the subordinate commands, click the corresponding top-level command. For example, to select the Paste command, first open the Edit menu. You can nest menu commands to more than two levels by selecting a command and pressing the button with the rig’it-pointing arrow more than once. When a me~u command leads to a submenu, an arrow appears to its right, indicating that if selected, it will lead to a submenu.

The remaining fields on the Menu Editor window are optional and are described next.

The Index Field

Use the Index field to create an array of menu commands. All the commands of the array have the same name and a unique index that distinguishes them, just like arrays of controls. When appending a list of recently opened filenames in the File menu, it’s customary to create an array of menu commands (one for each filename); all h. ave the same name and a differen.t index.

The Checked Field

Some menu commands act as toggles, and they are usually checked to indicate that they are on or unchecked to indicate that they are off. To display a checkmark

next to a menu command initially, select the command from the list by clickingits name, and then check the Checked box in the Menu Editor win dow. You can also access this property from within your code to change the checked status of a menu command at runtime. For example, to toggle the status of a menu command called FrmtBold, use the statement:

FrmtBo’l d. Checked – Not FrmtBo 1d. Checked

The Enabled Field

Some menu commands aren’t always available. The Paste command, for example, ‘pas no meaning if the Clipboard is empty. To indicate that a command can’t be used at the time, you set its Enabled property to False. The command then appears grayed in the menu, and although it can be highlighted, it can’rbe activated. You. can set the initial status of a command by checking or clearing the Enabled box in the Menu Editor window. You call also toggle the status of a menu command from within your code by manipulating its Enabled property, similar to the Checked command:

Paste C~P.Enabled – Not Paste CMP.Enabled

The Visible Field

To remove a command temporarily from the menu.set the command’s Visible property to False. The Visible property isn’t used frequently in menu design. In general, you should prefer to disable a command to indicate that it can’t be used (some other action is required to enable it). Making a command invisible frustrates users, who may try to locate the command in another menu.

The Window Field

This option is used with MDI (Multiple Document Interface) applications to maintain a list of all open windows. The Window List option is explained. The Multiple Document Interface.

The place to set menu control properties is the Menu Editor window. Each menu command is an object that belongs to the Form on which it appears (as are Com- .mand buttons). Therefore, it is possible to set a menu object’s properties through the Properties winrJow. You can’t, however, select a command on the. menu bar and then look up its properties in the Properties window. You must expand the list of objects at the top of the Properties window, select the ‘menu object by name, and then view (or change) the menu object’s properties ..Likewise, you ran program the Click event of each menu command by selecting the command’s name in the Object list of the Code window.

Posted on November 4, 2015 in Working with Forms

Share the Story

Back to Top
Share This