Menu Object for Visual Basic 6.0 Users
The Menu object in Visual Basic 6.0 is replaced by either the MenuStrip control or the ContextMenuStrip control in Visual Basic 2008. The names of some properties, methods, events, and constants are different, and in some cases there are differences in behavior.
In Visual Basic 6.0, a Menu object is created using the Menu Editor. Menu objects are tied to the form for which they are created, but they can be modified at run time or displayed as context menus. Menus created with the Menu object do not natively support shading, icons, or embedded controls; only Windows 98 "flat"-style menus can be created.
In Visual Basic 2008, the Menu object is replaced by the MenuStrip and ContextMenuStrip components. Menus may be created using in-place editing at design time or they may be created in code. The MenuStrip and ContextMenuStrip components allow you to create modern Office toolbar style menus, with full support for shaded regions, icons, and embedded controls such as dropdown lists. For more information, see MenuStrip Control Overview (Windows Forms).
Displaying Context Menus
In Visual Basic 6.0, you can display a context menu by calling the PopupMenu method and passing it any top level Menu object. For example, if you have an Edit menu named mnuEdit with Cut, Copy, and Paste submenus, you can display a context menu with the Cut, Copy, and Paste commands by calling PopupMenu mnuEdit.
In Visual Basic 2008, context menus are displayed using a separate ContextMenuStrip component. There is no equivalent for the PopupMenu method, however you can still share commands between a MenuStrip and a ContextMenuStrip by creating the ContextMenuStrip at design time and sharing event handlers.
The following examples illustrate differences in coding techniques between Visual Basic 6.0 and Visual Basic 2008.
Code Changes for Displaying Context Menus
The following code demonstrates how to display Cut, Copy, and Paste commands from an existing Edit menu in a context menu.
' Visual Basic 6.0 Private Sub mnuCut_Click() MsgBox "You selected Cut" End Sub Private Sub mnuCopy_Click() MsgBox "You selected Copy" End Sub Private Sub mnuPaste_Click() MsgBox "You selected Paste" End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = vbRightButton Then PopupMenu mnuEdit End If End Sub
' Visual Basic ' You must first add a ContextMenuStrip component to the form at design ' time and add Cut, Copy, and Paste menu items named ' CutContextMenuItem, CopyContextMenuItem, and PasteContextMenuItem. Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click MsgBox("Cut") End Sub Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click MsgBox("Copy") End Sub Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click MsgBox("Paste") End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As _ System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown If e.Button = Windows.Forms.MouseButtons.Right Then Me.ContextMenuStrip = ContextMenuStrip1 AddHandler CutContextMenuItem.Click, _ AddressOf CutToolStripMenuItem_Click AddHandler CopyContextMenuItem.Click, _ AddressOf CopyToolStripMenuItem_Click AddHandler PasteContextMenuItem.Click, _ AddressOf PasteToolStripMenuItem_Click End If End Sub
The following tables list Visual Basic 6.0 properties and methods, along with their Visual Basic 2008 equivalents. Those properties and methods that have the same name and behavior are not listed. All Visual Basic 2008 enumerations map to the System.Windows.Forms namespace unless otherwise noted.
This table provides links to topics explaining behavior differences. Where there is no direct equivalent in Visual Basic 2008, links are provided to topics that present alternatives.
Visual Basic 6.0
Visual Basic 2008 Equivalent
New implementation. For more information, see Help Support for Visual Basic 6.0 Users.
New implementation. See the Index property of the ToolStripItemCollection class.
No equivalent. This property was only used for OLE linking and embedding which is no longer supported.
Visual Basic 6.0
Visual Basic 2008 Equivalent
New implementation. Use a ContextMenuStrip component.