Ver versión en español    
 
Home
10 Reasons to use MZ-Tools
MZ-Tools 6.0 for VS.NET
Editions
Features
Online Documentation
MZ-Tools SDK
Download
Purchase
Version History (RSS)  
FAQ & Support
MZ-Tools 3.0 for VB6 & VBA
Features
Online Documentation
Download (freeware)
Donations (Paypal)
Version History (RSS)  
FAQ & Support
User Reviews
Community Place
Contact  
For Add-In Developers
About
   
 
User Testimonials

I'm an avid supporter of MZ-Tools. It's a product I couldn't do without and your level of support is outstanding.

Jan Hyde (Visual Basic MVP)

You will soon wonder how you ever lived without it.

Andy Maggs

More user reviews
 
HOWTO: Add a popup command bar to the context menu of a code window of Visual Studio .NET

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: August 2004   Microsoft Visual Studio .NET 2003
      Microsoft Visual Studio 2005
Introduction

When creating addins for Visual Studio .NET, sometimes you may want to add some menus in the context menu of a code window of Visual Studio .NET. This article shows the code to do it, using a popup command bar to group all the menus of your addin.

More Information

This VB.NET sample of an add-in adds 2 commands to Visual Studio .NET in the UISetup phase and when the add-in is loaded it creates a popup command bar in the context menu of the code window and adds a submenu for each command:

Public Class Connect
    Implements Extensibility.IDTExtensibility2
    Implements EnvDTE.IDTCommandTarget
 
    Private Const m_NAME_COMMAND1 As String = "MyCommand1"
    Private Const m_NAME_COMMAND2 As String = "MyCommand2"
 
    Private m_objDTE As EnvDTE.DTE
    Private m_objAddIn As EnvDTE.AddIn
 
    Private m_objMyCommandBarPopup As CommandBarPopup
    Private m_objMyCommandBarControl1 As CommandBarControl
    Private m_objMyCommandBarControl2 As CommandBarControl
 
    Public Sub OnBeginShutdown(ByRef custom As System.Array) _
        Implements Extensibility.IDTExtensibility2.OnBeginShutdown
    End Sub
 
    Public Sub OnAddInsUpdate(ByRef custom As System.Array) _
        Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
    End Sub
 
    Public Sub OnStartupComplete(ByRef custom As System.Array) _
        Implements Extensibility.IDTExtensibility2.OnStartupComplete
    End Sub
 
    Public Sub OnConnection(ByVal application As Object, _
        ByVal connectMode As Extensibility.ext_ConnectMode, _
        ByVal addInInst As Object, ByRef custom As System.Array) _
        Implements Extensibility.IDTExtensibility2.OnConnection
 
        Dim objMyCommandBarControl As CommandBarControl
        Dim objCodeWindowCommandBar As CommandBar

        Dim objCommand1 As Command
        Dim objCommand2 As Command
        Dim objCommand3 As Command
 
        Try
            m_objDTE = DirectCast(application, EnvDTE.DTE)
            m_objAddIn = DirectCast(addInInst, EnvDTE.AddIn)
 
            If (connectMode = ext_ConnectMode.ext_cm_UISetup) Then
 
                ' Create commands in the UI Setup phase. This phase is called only once.
                objCommand1 = m_objDTE.Commands.AddNamedCommand(m_objAddIn, m_NAME_COMMAND1, _
                    "MyCommand 1", "MyCommand 1", True, 59)

                objCommand2 = m_objDTE.Commands.AddNamedCommand(m_objAddIn, m_NAME_COMMAND2, _
                    "MyCommand 2", "MyCommand 2", True, 59)
 
            Else
 
                ' Retrieve commands 
                objCommand1 = m_objDTE.Commands.Item(m_objAddIn.ProgID & "." & m_NAME_COMMAND1)
                objCommand2 = m_objDTE.Commands.Item(m_objAddIn.ProgID & "." & m_NAME_COMMAND2)
 
                ' Retrieve the context menu of code windows
                objCodeWindowCommandBar = m_objDTE.CommandBars.Item("Code Window")
 
                ' Add a popup command bar
                objMyCommandBarControl = objCodeWindowCommandBar.Controls.Add( _
                    MsoControlType.msoControlPopup, System.Type.Missing, _
                    System.Type.Missing, System.Type.Missing, System.Type.Missing)
                m_objMyCommandBarPopup = DirectCast(objMyCommandBarControl, CommandBarPopup)
 
                ' Change its caption
                m_objMyCommandBarPopup.Caption = "My popup"
 
                ' Add controls to the popup command bar
                m_objMyCommandBarControl1 = objCommand1.AddControl(m_objMyCommandBarPopup.CommandBar)
                m_objMyCommandBarControl2 = objCommand2.AddControl(m_objMyCommandBarPopup.CommandBar)
 
            End If
 
        Catch objException As Exception
            MessageBox.Show(objException.ToString, "Error", _
               MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
 
    End Sub
 
    Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, _
        ByRef custom As System.Array) _
        Implements Extensibility.IDTExtensibility2.OnDisconnection
 
        Try

            If Not (m_objMyCommandBarPopup Is Nothing) Then
                m_objMyCommandBarPopup.Delete()
            End If
 
        Catch objException As Exception
            MessageBox.Show(objException.ToString, "Error", _
               MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
 
    End Sub
 
    Public Sub Exec(ByVal CmdName As String, ByVal ExecuteOption As EnvDTE.vsCommandExecOption, _
        ByRef VariantIn As Object, ByRef VariantOut As Object, ByRef handled As Boolean) _
        Implements EnvDTE.IDTCommandTarget.Exec
 
        Select Case CmdName
            Case m_objAddIn.ProgID & "." & m_NAME_COMMAND1
                MessageBox.Show("MyCommand1 executed")
 
            Case m_objAddIn.ProgID & "." & m_NAME_COMMAND2
                MessageBox.Show("MyCommand2 executed")
 
        End Select
 
    End Sub
 
    Public Sub QueryStatus(ByVal CmdName As String, _
        ByVal NeededText As EnvDTE.vsCommandStatusTextWanted, _
        ByRef StatusOption As EnvDTE.vsCommandStatus, ByRef CommandText As Object) _
        Implements EnvDTE.IDTCommandTarget.QueryStatus
 
        Select Case CmdName
 
            Case m_objAddIn.ProgID & "." & m_NAME_COMMAND1
                StatusOption = vsCommandStatus.vsCommandStatusSupported Or _
                    vsCommandStatus.vsCommandStatusEnabled
 
            Case m_objAddIn.ProgID & "." & m_NAME_COMMAND2
                StatusOption = vsCommandStatus.vsCommandStatusSupported Or _
                    vsCommandStatus.vsCommandStatusEnabled
 
        End Select
 
    End Sub
 
End Class

Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this

MZ-Tools 6.0 for Visual Studio .NET

You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:

Buy MZ-Tools Now Download MZ-Tools Demo

   Top