Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Create Visual Basic Context Menu Strip

0.00/5 (No votes)
17 Mar 2014 1  
How to create Visual Basic Context Menu Strip

Today, we will have a look at Visual Basic Context Menu design. Context Menus always come in handy for providing quickly accessible options to the end user. Follow the simple steps below to get started:

  1. Drag and Drop a Visual Basic Context Menu Strip on the Form Design:

  2. Add new options to ContextMenu using Edit Items:

  3. We want to add two Options in Context Menu. So add two new MenuItems using the Add button. Change the Text property to Maximize and Exit respectively for MenuItem1 and MenuItem2:

  4. Click OK to close dialog box. ContextMenuStrip is now ready to go!

  5. We can also set image (Icon) of the MenuItem. Just right click to see Maximize MenuItem options. Click Set Image and browse to an image file:

    We have added the image to the Maximize option:

  6. Just add the following simple code to the Form Code. It allows Windows Form to use ContextMenuStrip1 at run time:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
               Handles MyBase.Load
            Me.ContextMenuStrip = ContextMenuStrip1
        End Sub
    
    End Class

Run the application using F5. Right click to see context Menu Strip live.

Adding Functionality to Menu Items in Visual Basic Context Menu Strip

Modify the code as follows:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles MyBase.Load
        Me.ContextMenuStrip = ContextMenuStrip1

        'Add functionality for ToolStripMenuItem1 (Maximize) click
        AddHandler ToolStripMenuItem1.Click, AddressOf menuItem1_Click

        'Add functionality for ToolStripMenuItem2 (Exit) click
        AddHandler ToolStripMenuItem2.Click, AddressOf menuItem2_Click
    End Sub

    Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.WindowState = FormWindowState.Maximized
    End Sub

    Private Sub menuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub
End Class

At Form Load, we have added Handlers for Click events of Menu Items. Rest of the code is self-explanatory. Run the application and see click action yourself.

Coding for Context Menu

Context menus can also be added programmatically. We have already covered it in our post for Taskbar Notification in Visual Basic.

The post How to create Visual Basic Context Menu Strip appeared first on Bubble Blog.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here