The use of VB menu in the application (ii)

Source: Internet
Author: User
Tags arrays valid

Each menu created by

Create a submenu
can contain up to five levels of submenus. The submenu will split the other menu to show its own menu items. Where you need to use submenus are:
the menu bar is full.
A particular menu control is rarely used. The
to highlight the relationship of a menu control to another.
However, if there is room in the menu bar, it is best to create a menu title instead of a submenu. This allows all controls to be visible when the menu is pulled. Restricting the use of submenus is also a good programming strategy that eliminates the burden of finding the application's menu interface (most applications use only one level of submenus).
in the Menu editor, any menu control indented under a menu control that is not a menu caption is a submenu control. In general, child menu controls can include submenu items, separator bars, and submenu headings.
To create a submenu, follow these steps:
1. Create a menu item that you want to use as a submenu title.
2. Create individual items that will appear in the New submenu, and then click the right arrow button to indent them. The
adds four dots (...) before each indent level in the menu editor. To remove one indent level, click the left arrow button.
Note If you want to use more than level submenus, consider using a dialog box instead. The dialog box allows you to specify several choices in one place. For information about using the dialog box, see "dialog Box" later in this chapter.

The

Create Menu control array
Menu control array is a collection of menu items that share the same name and event procedure on the same menu. The Menu control array is used to:
to create a new menu item at run time, it must be a member of a control array. As the Mdinotepad example, it uses a Menu control array to store the newly opened file manifest. The
simplifies code because common blocks of code can be used by all menu items.
each Menu control array element is identified by a unique index value that is specified in the Index property box on the menu editor. When a control array member recognizes an event, Visual basic passes its Index property value as an additional parameter to the event procedure. The event procedure must contain code that checks the value of the Index property, so you can determine which control is being used.
For more information about control arrays, see "Using Control Arrays" in chapter seventh, "Standard controls with Visual Basic." To create an array of menu controls 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 comes up with the first menu title in the menu bar now. The menu title text appears in the Menu control list box.
4. In the Name text box, type the name that will be used in your code to reference the Menu control. Leave the index box empty.
5. At the next indent level, create a menu item that will become the first element in the array by setting the title and name.
6. Sets the index of the first element in the array to 0.
7. Creates a second menu item on the same indentation level as the first one.
8. Sets the second element's name to be the same as the first element, and sets its index to 1.
9. Repeat step 5-8 for subsequent elements in the array. The elements of the
Key menu control array must be contiguous in the Menu control list box and must be at the same indent level. When you create a menu control array, you include the separator bar that appears in the menu.

Create and modify menus at run time
Menus created at design time can also dynamically respond to run-time conditions. For example, if the action of a menu item becomes inappropriate at some point, the selection of the menu item can be prevented by invalidating it. For example, in an MDI NotePad application, if there is no text on the Clipboard, the Paste menu item on the Edit menu is dimmed so that it cannot be selected.
If you have an array of menu controls, you can also dynamically add menu items. This is described in "Adding a Menu control at run Time" later in this topic.
You can also write an application that uses a check mark to indicate which of several commands is last selected. For example, if the toolbar is displayed, the Options menu item for the MDI NotePad application toolbar displays a check mark. Other menu control features described in this section include code that makes menu items visible or invisible, and adds or deletes menu item actions.

Make a menu command valid or invalid
All menu controls have the Enabled property, and when this property is set to False, the menu command is invalid so that it does not respond to the action. When Enabled is set to False, access to the shortcut key is also invalid. An invalid menu control is dimmed, as shown in Figure 6.11, "Paste" menu item.

For example, the following statement invalidates the Paste menu item on the Edit menu in an MDI NotePad application.
mnueditpaste.enabled = False
The invalid menu title makes the entire menu invalid, because you cannot access any menu item without first clicking the menu title. For example, the following code will invalidate the Edit menu for an MDI NotePad application.
mnuedit.enabled = False

Display a check mark on a menu control
Using the Checked property, you can place a check mark on the menu to:
Represents the status of an open/close condition. Select the menu command to alternately Add and remove this check mark.
Indicates which of several modes is working. The Options menu of the MDI NotePad application uses a check mark to indicate the status of the toolbar, as shown in Figure 6.12.
You can use the Checked property to create a check mark in Visual Basic. Set the initial value of the Checked property in the menu editor by selecting the check box that is marked as Checked. To add or remove a check mark from a menu control at run time, you can set its checked property from code. For example:
Private Sub Mnuoptions_click ()
' Sets the state of the check mark based on the Visible property.
mnuoptionstoolbar.checked = pictoolbar.visible
End Sub

Make a Menu control invisible
In the menu editor, you can set the initial value of the Visible property of a menu control by selecting a check box that is marked as Visible. At run time, to make a menu control visible or invisible, you can set its Visible property from code. For example:
Mnufilearray (0). Visible = True ' makes the control visible.
Mnufilearray (0). Visible = False ' makes the control invisible.
When a menu control is not visible, the rest of the controls in the menu move up to fill the vacated space. If the control is on the menu bar, the rest of the controls on the menu bar move left to fill the space.
Note that making a menu control invisible also results in an invalid effect because the control is no longer accessible through menus, access keys, or shortcut keys. If the menu title is not visible, all controls on the menu are not valid.

Add a Menu control at run time
Run-time menus can grow. For example, in Figure 6.13, a file opened as an SDI NotePad application dynamically creates a menu item to display the pathname of the file you just opened.

The runtime must use a control array in order to create the control. Because the Index property of the Mnurecentfile menu control is assigned at design time, it automatically becomes an element of the control array-even if no other elements have been created.
When you create Mnurecentfile (0), you actually create a separator bar that is not visible at run time. When a runtime user stores a file for the first time, the separator bar becomes visible and the first file name is added to the menu. Every time a file is stored at runtime, a menu control is mounted into the array, which makes the menu grow.
Controls created at run time can be hidden by using the Hide method or by setting the control's Visible property to False. If you want to remove a control from an array of controls from memory, use the Unload statement.

Code to write a menu control
When the user selects a menu control, a Click event appears. You need to write a Click event procedure for each menu control in your code. All menu controls except the separator bar (and invalid or invisible menu controls) recognize the Click event.
The code written in the menu event process is identical to the code written in any other event procedure for the control. For example, the code for the Click event for the Close menu item in the File menu appears as follows:
Sub Mnufileclose_click ()
Unload Me
End Sub
When the menu caption is selected, Visual Basic automatically displays a menu, but there is no need to write code for the Click event procedure for a menu caption, unless you want to perform other actions, such as making certain menu items invalid each time the menu is displayed.
Note At design time, when you close the menu editor, the menu that you create appears on the form. Selecting a menu item on a form displays the Click event procedure for that menu control.

Show pop-up menu
A pop-up menu is a floating menu that is displayed on a form, independent of the menu bar. The items that appear on the pop-up menu depend on where the pointer is when you press the right mouse button, and therefore the pop-up menu is also called the context menu.
In Microsoft Windows 95, you can activate the context menu by right-clicking the right mouse button. At run time, any menus that contain at least one menu item can be displayed as pop-up menus. To display the pop-up menu, you can use the PopupMenu method. This method uses the following syntax:
[Object.] PopupMenu Menuname [, Flags [, x [, Y [, Boldcommand]]]]
For example, when the user right-clicks a form with the right mouse button, the following code displays a menu named Mnufile. A MouseUp or MouseDown event can be used to detect when the right mouse button is clicked, although the standard usage is to use the MouseUp event:
Private Sub form_mouseup (Button as Integer, Shift as _
Integer, X as single, Y as single
If button = 2 Then ' Check if the right mouse button is clicked.
PopupMenu mnufile ' Displays the File menu as a pop-up menu.
End If
End Sub
The code to invoke the PopupMenu method will not run until the menu is selected or the menu is canceled.
Note Only one pop-up menu can be displayed at a time. When a pop-up menu is displayed, the subsequent call to the PopupMenu method is ignored. Any time a menu control is active, invoking the PopupMenu method will not be ignored.
You will often want to use a pop-up menu to access those options that are not commonly used in the menu bar. To create a menu that does not appear in the menu bar, you can make the top-level menu item invisible at design time (guarantee that the "Visible" check box in the menu editor is not selected). When Visual Basic displays a pop-up menu, the Visible property of the specified top-level menu is ignored.

Flags parameter
Use the flags parameter in the PopupMenu method to further define the position and performance of the pop-up menu. The following table lists the flags that you can use to describe the location of the pop-up menu.

To specify a flag, select a constant from each group, and then connect them with the Or operator. The following code shows a pop-up menu with a top border in the center of the form when the user clicks a command button. The pop-up menu triggers the Click event for a menu item that is right or left-clicked.
Private Sub Command1_Click ()
' X and Y variables ' dimensions.
Dim Xloc, Yloc
' Set the X and Y variables to the center of the form.
Xloc = SCALEWIDTH/2
Yloc = SCALEHEIGHT/2

' Show pop-up menu.
PopupMenu Mnuedit, Vbpopupmenucenteralign or _
Vbpopupmenurightbutton, Xloc, Yloc
End Sub

Boldcommand parameters
Use the Boldcommand parameter to specify the name of the menu control that you want to appear in bold font in the pop-up menu that appears. Only one menu control in the pop-up menu is bold.

Menus in an MDI application
In an MDI application, each subform's menu is displayed on the MDI window s body, not the subform itself. When the subform has focus, the subform's menu (if any) replaces the MDI form menu on the menu bar. If there are no visible subforms, or if the subform with focus does not have a menu, the menu for the MDI form is displayed (see Figures 6.14 and 6.15). It is common for MDI applications to use several sets of menus. When you open a document, the application displays the menu associated with the class document. Typically, a different menu is displayed when no child forms are visible. For example, when no file is open, Microsoft Excel displays only the files and Help menus. When a user opens a file, other menus are displayed (file, edit, view, insert, format, tools, data, and windows, and so on).

Create a menu of MDI applications
You can create menus for Visual Basic applications by adding menu controls to MDI forms and subforms. One way to manage menus in an MDI application is to place a menu control that you want to display at any time on an MDI form, even when no subform is visible. When you run the application, if there are no visible subforms, the MDI form menu is automatically displayed, as shown in Figure 6.14.
Place the menu control applied to the subform in the subform. At run time, whenever a subform is visible, the menu headings are displayed in the menu bar of the MDI form.
Some applications support multiple types of documents. For example, in Microsoft Access, you can open tables, queries, forms, and other document types. To create such an application in Visual Basic, you should use two subforms. Design a subform with a menu of execution work orders and another subform with the complete Chart task menu.

At run time, when the instance of the work form has the focus, the work order menu is displayed, and the chart form's menu is displayed when the chart is selected. If all worksheets and charts are closed, the menu for the MDI form is displayed. For more information about creating menus, see the "Using Menus in Applications" section earlier in this chapter.

Create the Window menu
Most MDI applications (for example, Microsoft Word for Windows and MicrosoftExcel) combine the Window menu. This is a special menu that shows the title of all open subforms, as shown in Figure 6.15. In addition, some applications will manipulate the commands of the subform, such as "cascade", "Tile" and "Arrange Icons", and put them in this menu.
Any menu control on an MDI form or MDI child form can be used to display a manifest that opens a subform, as long as its Windowlist property is set to True. At run time, VisualBasic automatically manages and displays a list of headings and displays a check mark next to the title that currently has focus. In addition, a separator bar is automatically placed above the window manifest.
To set the Windowlist property, follow these steps:
1. Select the form on which you want the menu to appear, and from the Tools menu, choose Menu Editor.
Note The Windowlist property applies only to MDI forms and MDI child forms. It does not work on standard forms (not MDI).
2. In the Menu Editor list box, select the menu on which you want to open the list of subforms to display.
3. Select the Windowlist check box.
At run time, this menu displays a list of open subforms. In addition, the Windowlist property of this menu control returns TRUE.
For more information, see the "Windowlist properties" of the language reference.

Arrange the child forms
As mentioned earlier, some applications list actions such as tiling (Tile), cascading (Cascade) and arranging icons (arrange Icons) on the same menu as the Open subform list. Use the Arrange method in the MDI form to Zizi the form again. You can cascade, tile horizontally, or follow a subform icon arranged along the lower part of an MDI form to display the subform. The following example shows the Click event procedure for the Cascade, tile, and Arrange Icons menu controls.
Private Sub Mnuwcascade_click ()
' Cascade the subform.
Frmmdi.arrange Vbcascade
End Sub

Private Sub Mnuwtile_click ()
' Flat shop form.
Frmmdi.arrange Vbtilehorizontal
End Sub

Private Sub Mnuwarrange_click ()
' Arranges all child form icons.
Frmmdi.arrange vbarrangeicons
End Sub
Note Internal constants Vbcascade, Vbtilehorizontal, and Vbarrangeicons are listed in the Visual Basic (VB) object Library of the Object Browser.
When you tile or cascade a subform with a fixed border type, each subform is positioned as if it has a variable size border. This makes the subform possible to overlap.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.