Click here to Skip to main content
15,886,049 members
Articles / Programming Languages / Visual Basic

How to Create Visual Basic Context Menu Strip

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
17 Mar 2014CPOL1 min read 47.5K   1   3
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)


Written By
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/

Comments and Discussions

 
PraiseJust what I needed Pin
Member 141005119-Mar-19 21:21
Member 141005119-Mar-19 21:21 
QuestionHow do you deal with ContextMenus that produce duplicates? Pin
Member 1178889915-Dec-15 7:58
Member 1178889915-Dec-15 7:58 
AnswerRe: How do you deal with ContextMenus that produce duplicates? Pin
Member 1178889915-Dec-15 8:43
Member 1178889915-Dec-15 8:43 
I found a solution, and that was to create a variable instead of using the item in items in the for statement. My only problem now is this error am getting:

VB.NET#
An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll



It is generated from here:

VB.NET
If nextCheckIndex = replacements.Count Then
       MessageBox.Show("Check complete.")
   Else

       Dim checkWord = replacements.Keys.ElementAt(nextCheckIndex)

       Dim foundIndex = RichTextBox1.Find(checkWord, 0, RichTextBox1.TextLength, RichTextBoxFinds.WholeWord)
       If foundIndex > -1 Then

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.