How to Create Visual Basic Context Menu Strip





5.00/5 (1 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:
- Drag and Drop a Visual Basic Context Menu Strip on the Form Design:
- Add new options to
ContextMenu
using Edit Items: - We want to add two Options in Context Menu. So add two new
MenuItem
s using the Add button. Change theText
property toMaximize
andExit
respectively forMenuItem1
andMenuItem2
: - Click OK to close dialog box.
ContextMenuStrip
is now ready to go! - We can also set image (
Icon
) of theMenuItem
. Just right click to see MaximizeMenuItem
options. Click Set Image and browse to an image file:We have added the image to the
Maximize
option: - 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.