Click here to Skip to main content
12,509,148 members (60,219 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

15.9K views
1 bookmarked
Posted

How to Create Visual Basic Context Menu Strip

, 17 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

    Visual Basic Context Menu

  2. Add new options to ContextMenu using Edit Items:

    Visual Basic Context Menu

  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:

    Visual Basic Context Menu

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

    Visual Basic Context Menu

  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:

    Visual Basic Context Menu

    We have added the image to the Maximize option:

    Visual Basic Context Menu

  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. :)

Visual Basic Context Menu

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, 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/

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionHow do you deal with ContextMenus that produce duplicates? Pin
Member 1178889915-Dec-15 7:58
memberMember 1178889915-Dec-15 7:58 
AnswerRe: How do you deal with ContextMenus that produce duplicates? Pin
Member 1178889915-Dec-15 8:43
memberMember 1178889915-Dec-15 8:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 17 Mar 2014
Article Copyright 2014 by Sharjeel_Ahmad
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid