Condividi tramite


Visual Basic Concepts

Creating a Menu Control Array

A menu control array is a set of menu items on the same menu that share the same name and event procedures. Use a menu control array to:

  • Create a new menu item at run time when it must be a member of a control array. The MDI Notepad sample, for example, uses a menu control array to store a list of recently opened files.

  • Simplify code, because common blocks of code can be used for all menu items.

Each menu control array element is identified by a unique index value, indicated in the Index property box on the Menu Editor. When a member of a control array recognizes an event, Visual Basic passes its Index property value to the event procedure as an additional argument. Your event procedure must include code to check the value of the Index property, so you can determine which control you're using.

**For More Information   **For more information on control arrays, see "Working with Control Arrays" in "Using Visual Basic's Standard Controls."

To create a menu control array in the Menu Editor

  1. Select the form.

  2. From the Tools menu, choose Menu Editor.

    –or–

    Click the Menu Editor button on the toolbar.

  3. In the Caption text box, type the text for the first menu title that you want to appear on the menu bar.

    The menu title text is displayed in the menu control list box.

  4. In the Name text box, type the name that you will use to refer to the menu control in code. Leave the Index box empty.

  5. At the next indentation level, create the menu item that will become the first element in the array by setting its Caption and Name.

  6. Set the Index for the first element in the array to 0.

  7. Create a second menu item at the same level of indentation as the first.

  8. Set the Name of the second element to the same as the first element and set its Index to 1.

  9. Repeat steps 5 – 8 for subsequent elements of the array.

**Important   **Elements of a menu control array must be contiguous in the menu control list box and must be at the same level of indentation. When you're creating menu control arrays, be sure to include any separator bars that appear on the menu.