Udostępnij za pośrednictwem


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.

Conceptual Differences

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.

Code Changes for the Menu Object

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.PrivateSub CutToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
    MsgBox("Cut")
EndSubPrivateSub CopyToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
    MsgBox("Copy")
EndSubPrivateSub PasteToolStripMenuItem_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
    MsgBox("Paste")
EndSub
PrivateSub Form1_MouseDown(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.MouseEventArgs) HandlesMe.MouseDown

  If e.Button = Windows.Forms.MouseButtons.Right ThenMe.ContextMenuStrip = ContextMenuStrip1

    AddHandler CutContextMenuItem.Click, _
      AddressOf CutToolStripMenuItem_Click

    AddHandler CopyContextMenuItem.Click, _
      AddressOf CopyToolStripMenuItem_Click

    AddHandler PasteContextMenuItem.Click, _
      AddressOf PasteToolStripMenuItem_Click
  EndIfEndSub

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.

Properties

Visual Basic 6.0

Visual Basic 2008 Equivalent

Caption

Text (MenuStripItem)

HelpContextID

New implementation. For more information, see Help Support for Visual Basic 6.0 Users.

Index

New implementation. See the Index property of the ToolStripItemCollection class.

NegotiatePosition

No equivalent. This property was only used for OLE linking and embedding which is no longer supported.

Parent

FindForm method

WindowList

MdiWindowListItem

Methods

Visual Basic 6.0

Visual Basic 2008 Equivalent

PopupMenu

New implementation. Use a ContextMenuStrip component.

Upgrade Notes

When a Visual Basic 6.0 application is upgraded to Visual Basic 2008, any calls to the PopupMenu method are not upgraded; you must create context menus to replace these.

See Also

Concepts

Menu Handling for Visual Basic 6.0 Users

Reference

MenuStrip Control Overview (Windows Forms)

Other Resources

Windows Forms Controls for Visual Basic 6.0 Users