Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

How to create Visual Basic Context Menu Strip

, 17 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Today we will have a look at Visual Basic Context Menu design. Context Menus always come handy for providing quickly accessible options to the end user. Follow the simple steps …The post How to create Visual Basic Context Menu Strip appeared first on Bubble Blog.

Today we will have a look at Visual Basic Context Menu design. Context Menus always come 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

Visual Basic Context Menu

  1. Add new options to ContextMenu using Edit Items

Visual Basic Context Menu

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

Visual Basic Context Menu

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

Visual Basic Context Menu

  1. 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

Visual Basic Context Menu

We have added the image to the Maximize option

Visual Basic Context Menu

  1. Just add 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 :)

Visual Basic Context Menu

Adding Functionality to Menu Items in Visual Basic Context Menu Strip

Modify the code as following:

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 is Visual Basic.

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sharjeel_Ahmad
Engineer http://bubbleblog.net/
Pakistan Pakistan
A Telecom Engineer, Visual Basic enthusiast and Android news follower. He is contributing to an online blog as content writer and administrator
You can visit him here: http://bubbleblog.net/
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 17 Mar 2014
Article Copyright 2014 by Sharjeel_Ahmad
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid