ben2ong2
Quality Poster
Paid
Hero Member
   
Reputation: 17
Offline
Gender: 
Posts: 2374
9976.80 RD$
View Inventory
Send Money to ben2ong2
|
 |
« on: October 07, 2006, 03:53:21 PM » |
|
Menu controls provide a great way to offer users functionality needed to perform various tasks. The ability to control your menus in runtime also comes in handy. For instance, your application might show a list of recently opened files in a way that's similar to how Microsoft applications work. We'll look at setting up menus in design-time and making changes in runtime to give your applications more flexibility. You can download the sample code for this article here. advertisement
Menu control basics Menu controls allow displaying custom menu items for your application. Your menus can include regular menu items, dividers, up to four levels of submenus, and pop-up menus.
The Menu Editor offers the tools you need to create your menu controls. To access the Menu Editor, just click Tools | Menu Editor or press [Ctrl]E when a form is displayed in design-time. Alternatively, you can click the Menu Editor button. In addition to creating menus in Menu Editor, you can view and set their properties. Of course, you can accomplish the same thing by clicking on a menu's name in the Properties window.
Modifying menus in runtime Menus created in design-time can be modified in runtime. For example, if you set up a menu item mnuFile in design-time, you can run the following code to disable it: mnuFile.Enabled = False
You can check the menu item with the following code: mnuFile.Checked = True
A value of False will uncheck it.
Adding submenu items at runtime Creating submenus in runtime lets you provide users with the options they need in various situations. You have to create the first item in design-time, but you can dynamically add items in runtime using a control array. You can hide the items you create in runtime using Hide method or setting the Visible property to False.
Let’s code To see how easy it is to add runtime menu items, we'll create a simple VB project. Start by firing up VB and starting a new project. Next, go to the Menu Editor and add two elements: mnuFile and mnuFilesList, as shown in Figure A.
Figure A Menu Editor screen
Now, add two command buttons (cmdFill and cmdRemove) with the labels Add Menu Items and Remove Menu Items. Your screen should look like Figure B.
Figure B Runtime menu buttons
Add the following code to the cmdFill_Click() event: Private Sub cmdFill_Click() FillMenu 'Add items End Sub
Then, add this code to the cmdRemove_Click() event: Private Sub cmdRemove_Click() EmptyMenu 'Clear menu items End Sub
Now, add Private Sub FillMenu() with the following code: Private Sub FillMenu() Dim i As Integer Call EmptyMenu 'Clean before adding items
For i = 1 To 3 ' Add 3 new items to the menu Load mnuFilesList(i) ' Load new menu item mnuFilesList(i).Caption = "File" & i ' Set captions for menu items Next I mnuFilesList(0).Visible = False ' This is the divider - make it invisible
End Sub
Finally, add Private Sub EmptyMenu() with this code: Private Sub EmptyMenu() 'Empty the menu completely but leave the divider (created in design-time)- Dim i As Integer mnuFilesList(0).Visible = True ' Make 'parent' menu item visible. For i = 1 To mnuFilesList.UBound 'Remove items that were added in runtime Unload mnuFilesList(i) ' But keep the divider that was created in design-time Next I End Sub
Press [Ctrl][F5] to run the project. If you click on the File menu, all you'll see is the divider, as shown in Figure C. But when you click on the Add Menu Items button and then click on the File menu again, you will see items File1, File2, File3, as shown in Figure D.
Figure C Screen with runtime buttons
Figure D Added menu items
How does this work In design-time, we added two menu items, mnuFile, which is the menu itself, and mnuFilesList, which is a control array. We also created the first item in the array (a divider) with an Index property of 0. This item is needed to initialize the control array of menu items.
On the cmdFill_Click event, we call FillMenu, a procedure that adds the items to the control array. On the cmdRemove_Click event, we call EmptyMenu, a procedure that removes the items that were added in runtime and leaves only the divider.
Let’s now look at what happens in FillMenu and EmptyMenu. In FillMenu, we start by calling EmptyMenu to clean up the menu. Then, we create a loop to add three items to mnuFilesList, and we assign to them the values File1, File2, and File3. In our simplified example, we hard-coded these values. To add these values, we loaded an additional item and specified its Caption property. The items were loaded at runtime. We also set the Visible property of mnuFilesList to False so that the divider we created in design-time will be invisible. In EmptyMenu, we have a loop that goes through all the items in mnuFilesList and unloads them so that they are removed from the menu. We set the Visible property for the first item (the divider) to True.
|