Creating a Context Menu at Runtime

You can add a context menu programmatically to a Windows form in several steps: 1. Create a new public procedure, which in this example will be called AddContextMenu:

2. Public Sub AddContextMenu()

3. 'code goes here

4. End Sub

5. Within that procedure, declare an instance of a ContextMenu object.

In this example, the instance will be named mnuContextMenu.

Dim mnuContextMenu as New ContextMenu()

6. Set the ContextMenu property of the form to the MainMenu instance you just created: Me.ContextMenu = mnuContextMenu

The procedure now should read:

Public Sub AddContextMenu()

Dim mnuContextMenu as New ContextMenu()

Me.ContextMenu = mnuContextMenu

End Sub

Adding Items to the Context Menu

Once you have added a ContextMenu component to your Windows form, the next ste p is to add menu items to it. You can do this at design time, runtime, or by copying menu items from a MainMenu.

Was this article helpful?

0 0

Post a comment