To get this code working, drop a MenuStrip, ContextMenuStrip and a Button on the form, and then past the following code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetMenus()
End Sub
Private Sub SetMenus()
Dim mnuMenuItem1 As ToolStripMenuItem, mnuMenuItem3 As ToolStripMenuItem
mnuMenuItem1 = New ToolStripMenuItem("Item 1", Nothing, New EventHandler(AddressOf mnuMenuItem1_Click))
MenuStrip1.Items.Add(mnuMenuItem1)
mnuMenuItem3 = New ToolStripMenuItem("Item 3", Nothing, New EventHandler(AddressOf mnuMenuItem3_Click))
MenuStrip1.Items.Add(mnuMenuItem3)
Dim mnuContext1 As ToolStripMenuItem
Dim mnuContext2 As ToolStripMenuItem
mnuContext1 = New ToolStripMenuItem("ToolStripMenuItem1", Nothing, New EventHandler(AddressOf mnuContext1_Click))
mnuContext2 = New ToolStripMenuItem("ToolStripMenuItem2", Nothing, New EventHandler(AddressOf mnuContext2_Click))
ContextMenuStrip1.Items.Add(mnuContext1)
ContextMenuStrip1.Items.Add(mnuContext2)
End Sub
Private Sub mnuMenuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("Menu Item 1 Clicked")
End Sub
Private Sub mnuMenuItem3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("Menu Item 3 Clicked")
End Sub
Private Sub mnuContext1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("mnuContext1 Clicked")
End Sub
Private Sub mnuContext2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("mnuContext2 Clicked")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.ContextMenuStrip1.Show(Button1, New Point(0, Button1.Height))
End Sub
End Class
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment