Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Sapphire Soltuions

Use eVB to create a Menubar on your Pocket PC.

Written by Arnold Cota  [author's bio]  [read 43393 times]
Edited by Derek

Page 1  Page 2 

Using the MenuBar

Ok, now we are ready to look specifically at the Menu Bar control. First, it is usually a good idea to look at the online documentation for a control (if any exists). This will allow you to familiarize yourself with the properties, methods, and events of the control. To access the help use the menu Help->Contents. In this dialog, select the Index tab and type MenuBar, and you should see the relevant help entries.

Next, lets take a look at the properties of the MenuBar using the eVB properties sheet. Click on your MenuBar control on your form and take a look at the properties:

There arent too many we care about. MenuBar is a non-visual control meaning it does not show up on the form at runtime. For this reason, you can place it anywhere you like on the form and the Top and Left properties dont really matter. The only properties we care about are:

Name used to name the control

Enabled used to enable/disable the entire menu

NewButton used to set whether or not the PocketPC New menu to be displayed.

For our example, lets set NewButton to false to prevent the display of the PocketPC New menu.

Programming

Lets begin programming our MenuBar control. The MenuBar control is configured via code, so lets get into the form code editor. To access the code, either double click on the form in the visual form editor or select the form in the project explorer and click on the View Code icon. Normally, you want to set up your menu on form load so lets add an implementation of Form Load. To do this, select Form in the left combo box and Load in the right combo box. You should see your cursor positioned in the Form_Load sub.

I usually like to do my menu code in a subroutine called setupMenu. So lets add the following code to the form, then we will talk about it in detail. Note: Leave off the italicized text, it is just for your information:

To the top of the form add:

' Variables
' The File Menu
Private mnuFile As MenuBarMenu
' The Help Menu
Private mnuHelp As MenuBarMenu

Modify Form_Load

Private Sub Form_Load()
' Call the setup subroutine
setupMenu
End Sub

A new subroutine

Private Sub setupMenu()
'Set this at design time in the properties sheet
'menuBar.NewButton = False
' Set up a reference to the File Menu object
Set mnuFile = menuBar.Controls.AddMenu("File", "mnuFile")
' Begin adding menu items to the File menu
mnuFile.Items.Add 1, "mnuFileHello", "Say Hello"
' Create a menu separator
mnuFile.Items.Add 2, "mnuSep1", ""
mnuFile.Items(2).Style = mbrMenuSeparator
mnuFile.Items.Add 3, "mnuFileExit", "Exit"
' Set up a reference to the Help Menu object
Set mnuHelp = menuBar.Controls.AddMenu("Help", "mnuHelp")
' Begin adding menu items to the Help menu
mnuHelp.Items.Add 1, "mnuHelpAbout", "About"
End Sub

Lets discuss each area of code in turn. First, lets examine the declarations at the top of the form. For each menu that you would like to display, you need to declare an object type MenuBarMenu. For our example, we will have a File and a Help menu. Next, we added a call to our subroutine in Form_Load. Finally, we wrote our menu creation code in Sub setupMenu.

This line creates an actual instance of MenuBarMenu off of our MenuBar control:

Set mnuFile = mbMain.Controls.AddMenu("File", "mnuFile")

It takes two parameters, the first is the text to display on the menu, the second is the symbolic name to reference this menu by, or its key. Once we have created the menu object, we can start adding menu entries to it using this syntax:

mnuFile.Items.Add 1, "mnuFileHello", "Say Hello"

The Items collection is the collection of submenu objects that hang of a given menu. To add a new submenu item, use the <menu>.Items.Add method like we did above. It takes three parameters. First, is the index, second is the symbolic name (key), third is the menu text to display. You can also create menu separators the same way, with one added twist:

mnuFile.Items(2).Style = mbrMenuSeparator

You need to modify the style of the item you add. You can also use style to set up a checkbox menu item using the value of mbrCheck. See the online documentation for more details.

Run!

We are ready to run! Just click on the Run button to see the results:

Ok, so now we have menus. The next step is to write the code to react to the menu selections. The MenuBar control has a couple of events that it raises in response to user input. The one we want to implement is menuBar_MenuClick.

Private Sub menuBar_MenuClick(ByVal Item As MenuBarLib.Item)
' React to a menu selection
Select Case Item.Key
Case "mnuFileHello"
MsgBox "Hi, err, Hello!", vbOKOnly, "Say Hello"
Case "mnuFileExit"
' End the program
App.End
Case "mnuHelpAbout"
MsgBox "MenuBar Example 1.0", vbOKOnly, "About"
End Select
End Sub

This event is raised when a user clicks on one of your menu items. The item selected comes into the subroutine as an input parameter. In our function, we perform a select case statement based on the key of the Item. The key of the item was set when we created the items in setupMenu. In our example, we will perform a different action based on which menu item is selected.

Run and test

Go ahead and run the application now, and play with the menus and verify that what we expect to happen happens when you click on a menu item.

Conclusion

That about covers the introduction to the MenuBar control. There are many more features to discuss (enable/disable items, grouped menus, toolbar buttons, etc.) and I will discuss them in a follow up article on intermediate/advanced menu programming.

Previous Page