Results 1 to 16 of 16

Thread: [RESOLVED] [2005] vs addin doesn't work

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Resolved [RESOLVED] [2005] vs addin doesn't work

    i got most of this code from a link given to me in a previous thread. i think i understand it + i'm surprised it doesn't work. the addin doesn't do anything yet, i'm still working on adding an edit menuitem + an edit contextmenuitem. thats the part that doesn't work. i'll be grateful for any help. thanks.

    vb.net Code:
    1. Imports System
    2. Imports System.Collections.Generic
    3. Imports System.IO
    4. Imports System.Runtime.InteropServices
    5. Imports System.Web
    6. Imports System.Windows.Forms
    7. Imports Microsoft.VisualStudio.CommandBars
    8. Imports Extensibility
    9. Imports EnvDTE
    10. Imports EnvDTE80
    11.  
    12. Public Class Connect
    13.  
    14.     Implements IDTExtensibility2
    15.     Implements IDTCommandTarget
    16.  
    17.     Dim _applicationObject As DTE2
    18.     Dim _addInInstance As AddIn
    19.  
    20.     Private controls As List(Of CommandBarControl)
    21.     Private pasteCommand As Command
    22.     Private Shared contextGuids As Object() = New Object() {}
    23.     Private Shared contextMenuNames As String() = New String() {"Code Window"}
    24.  
    25.     '''<summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
    26.     Public Sub New()
    27.         Me.controls = New List(Of CommandBarControl)()
    28.     End Sub
    29.  
    30.     '''<summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
    31.     '''<param name='application'>Root object of the host application.</param>
    32.     '''<param name='connectMode'>Describes how the Add-in is being loaded.</param>
    33.     '''<param name='addInInst'>Object representing this Add-in.</param>
    34.     '''<remarks></remarks>
    35.     Public Sub OnConnection(ByVal application As Object, ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    36.         _applicationObject = CType(application, DTE2)
    37.         _addInInstance = CType(addInInst, AddIn)
    38.  
    39.         DeleteControls()
    40.         AddCommands()
    41.         AddControls()
    42.  
    43.     End Sub
    44.  
    45.     '''<summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
    46.     '''<param name='disconnectMode'>Describes how the Add-in is being unloaded.</param>
    47.     '''<param name='custom'>Array of parameters that are host application specific.</param>
    48.     '''<remarks></remarks>
    49.     Public Sub OnDisconnection(ByVal disconnectMode As ext_DisconnectMode, ByRef custom As Array) Implements IDTExtensibility2.OnDisconnection
    50.         DeleteControls()
    51.     End Sub
    52.  
    53.     '''<summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification that the collection of Add-ins has changed.</summary>
    54.     '''<param name='custom'>Array of parameters that are host application specific.</param>
    55.     '''<remarks></remarks>
    56.     Public Sub OnAddInsUpdate(ByRef custom As Array) Implements IDTExtensibility2.OnAddInsUpdate
    57.     End Sub
    58.  
    59.     '''<summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
    60.     '''<param name='custom'>Array of parameters that are host application specific.</param>
    61.     '''<remarks></remarks>
    62.     Public Sub OnStartupComplete(ByRef custom As Array) Implements IDTExtensibility2.OnStartupComplete
    63.     End Sub
    64.  
    65.     '''<summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
    66.     '''<param name='custom'>Array of parameters that are host application specific.</param>
    67.     '''<remarks></remarks>
    68.     Public Sub OnBeginShutdown(ByRef custom As Array) Implements IDTExtensibility2.OnBeginShutdown
    69.     End Sub
    70.  
    71.     Public Sub QueryStatus(ByVal commandName As String, ByVal neededText As vsCommandStatusTextWanted, ByRef status As vsCommandStatus, _
    72.     ByRef commandText As Object) Implements IDTCommandTarget.QueryStatus
    73.  
    74.         Try
    75.             If neededText = vsCommandStatusTextWanted.vsCommandStatusTextWantedNone Then
    76.                 If Me._applicationObject.ActiveDocument Is Nothing Then
    77.                     status = vsCommandStatus.vsCommandStatusUnsupported
    78.                 ElseIf (Me._applicationObject.ActiveDocument.Selection Is Nothing) OrElse DirectCast(Me._applicationObject.ActiveDocument.Selection, TextSelection).IsEmpty Then
    79.                     status = vsCommandStatus.vsCommandStatusSupported
    80.                 Else
    81.                     status = DirectCast((vsCommandStatus.vsCommandStatusSupported Or vsCommandStatus.vsCommandStatusEnabled), vsCommandStatus)
    82.                 End If
    83.             End If
    84.         Catch e As Exception
    85.             'MessageBox.Show([String].Format(Resources.CaughtExceptionMessage, e), Resources.CaughtExceptionCaption, MessageBoxButtons.OK, MessageBoxIcon.[Error])
    86.         End Try
    87.  
    88.     End Sub
    89.  
    90.     Public Sub Exec(ByVal commandName As String, ByVal executeOption As vsCommandExecOption, ByRef varIn As Object, ByRef varOut As Object, _
    91.     ByRef handled As Boolean) Implements IDTCommandTarget.Exec
    92.         Try
    93.             handled = False
    94.             If executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault Then
    95.  
    96.                 'Copy(False)
    97.                 'paste vbcode here
    98.                 Dim rtb As New RichTextBox
    99.                 rtb.Paste()
    100.                 rtb.SelectAll()
    101.                 rtb.Cut()
    102.                 ' TODO: paste clipboard contents into code editor
    103.                 handled = True
    104.  
    105.             End If
    106.         Catch e As Exception
    107.             'MessageBox.Show([String].Format(Resources.CaughtExceptionMessage, e), Resources.CaughtExceptionCaption, MessageBoxButtons.OK, MessageBoxIcon.[Error])
    108.         End Try
    109.  
    110.     End Sub
    111.  
    112.     Private Sub AddCommands()
    113.         If Me.pasteCommand Is Nothing Then
    114.             Me.pasteCommand = AddCommand("Paste_VBCode.connect.Paste_VBCode", "Paste_VBCode", "Paste VBCode", String.Empty, 0)
    115.         End If
    116.     End Sub
    117.  
    118.     Private Function AddCommand(ByVal fullCommandName As String, ByVal partialCommandName As String, ByVal buttonText As String, ByVal tooltip As String, ByVal icon As Integer) As Command
    119.  
    120.         Dim command As Command
    121.  
    122.         command = FindCommand(fullCommandName)
    123.         If command Is Nothing Then
    124.             command = DirectCast(Me._applicationObject.Commands, Commands2).AddNamedCommand2(Me._addInInstance, partialCommandName, buttonText, tooltip, True, icon, _
    125.              contextGuids, CInt(vsCommandStatus.vsCommandStatusSupported) + CInt(vsCommandStatus.vsCommandStatusEnabled), CInt(vsCommandStyle.vsCommandStyleText), vsCommandControlType.vsCommandControlTypeButton)
    126.         End If
    127.         Return command
    128.  
    129.     End Function
    130.  
    131.     Private Sub AddControls()
    132.  
    133.         Dim commandBars As CommandBars
    134.         Dim editPopup As CommandBarPopup
    135.         Dim copyIndex As Integer
    136.  
    137.         commandBars = DirectCast(Me._applicationObject.CommandBars, CommandBars)
    138.         editPopup = DirectCast(commandBars("MenuBar").Controls("Edit"), CommandBarPopup)
    139.  
    140.         copyIndex = FindPasteOnCommandBar("Edit") + 1
    141.  
    142.         Me.controls.Add(DirectCast(Me.pasteCommand.AddControl(editPopup.CommandBar, copyIndex), CommandBarControl))
    143.  
    144.         For Each contextMenuName As String In contextMenuNames
    145.             copyIndex = FindPasteOnCommandBar(contextMenuName) + 1
    146.             Me.controls.Add(DirectCast(Me.pasteCommand.AddControl(commandBars(contextMenuName), copyIndex), CommandBarControl))
    147.         Next
    148.  
    149.     End Sub
    150.  
    151.     Private Sub DeleteControls()
    152.         If Me.controls.Count > 0 Then
    153.             For Each control As CommandBarControl In Me.controls
    154.                 Try
    155.                     control.Delete(False)
    156.                 Catch
    157.                 End Try
    158.             Next
    159.             Me.controls.Clear()
    160.         End If
    161.     End Sub
    162.  
    163.     Private Function FindCommand(ByVal name As String) As Command
    164.         For Each command As Command In DirectCast(Me._applicationObject.Commands, Commands2)
    165.             If command.Name = name Then
    166.                 Return command
    167.             End If
    168.         Next
    169.         Return Nothing
    170.     End Function
    171.  
    172.     Private Function FindPasteOnCommandBar(ByVal commandBarName As String) As Integer
    173.  
    174.         Dim commandBars As CommandBars
    175.         Dim index As Integer
    176.  
    177.         commandBars = DirectCast(Me._applicationObject.CommandBars, CommandBars)
    178.         For index = 1 To commandBars(commandBarName).Controls.Count
    179.             If commandBars(commandBarName).Controls(index).Caption.Replace("&", "").Trim = "Paste" Then
    180.                 Return index
    181.             End If
    182.         Next
    183.         Return 0
    184.  
    185.     End Function
    186.  
    187. End Class

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] vs addin doesn't work

    Hey,

    I am not sure how the two differ, but I have just created my own add-in, based off the CopySourceAsHTML sample that I pointed you too, and I have been able to get the menu item to successfully appear. It appear directly under the CopySourceAsHTML menu item.

    I have attached the connect.vb file that I have created. It doesn't do anything else apart from putting a MenuItem in the ContextMenu with the text "Test".

    Hope this helps!!!

    Gary
    Attached Files Attached Files

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] vs addin doesn't work

    thanks... that would be great, but it doesn't work.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] vs addin doesn't work

    Hey,

    How do you mean it doesn't work?

    I tested the above before I posted it, and the menu item appeared in the correct place.

    Gary

  5. #5

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] vs addin doesn't work

    i copied it and added it to an addin project. the menuitems don't appear. could you post a complete working zipped example project?

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] vs addin doesn't work

    Hey,

    Sure, please find attached a zipped version of the application that I created, along with a screen shot showing the new menu item.

    The tests that I used to get this working were as follows:

    1. Open the attached solution.
    2. Start to debug
    3. In the instance of Visual Studio that is opened, open up an existing document.
    4. In the code window, right click to see the new menu item.

    Hope this helps!

    Gary
    Attached Images Attached Images  
    Attached Files Attached Files

  7. #7

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] vs addin doesn't work

    the file VBVSAddmin - For Testing.AddIn is missing.
    otherwise, its still not working. i think it might be some setting in my vs setup

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] vs addin doesn't work

    Hmmm, I don't know what else to suggest then. I have copied all the files from the project directory into that zip file, and posted them here.

    Just out of interest, when you created the Add-In, what options did you select?

    Gary

  9. #9

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] vs addin doesn't work

    i tried different options in several addins. but i tried your addin that you posted, and it didn't work. i have noticed, with the CopySourceAsHTML code from the link in the previous thread, it didn't work in debug, but the compiled project when installed with the installer, worked.

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] vs addin doesn't work

    So it is possible then that this is a Visual Studio setting that is preventing this from working.

    I am not aware of changing any of the default settings for my visual studio instance, apart from maybe to show line numbers in the IDE. Are you aware of any changes being made to yours?

    Not that this should make any difference, but what OS are you using? I am on XP SP2.

    What version of visual studio are you using? I have Version 8.0.50727.762 (SP.050727-7600), taken from the About screen.

    Gary

  11. #11

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] vs addin doesn't work

    visual studio Version 8.0.50727.867 (vsvista.050727-8600).
    i'm using vista home premium x64.

    i have noticed that some of the programs i've written in winXP don't work the same in vista.

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] vs addin doesn't work

    Hmmm, in order to rule that out, is there any chance you have access to another machine that has XP on it? If not, can you create a Virtual Machine? Maybe a little bit over the top, but would be good to get to the bottom of it.

  13. #13

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] vs addin doesn't work

    no. i haven't got access to xp.
    i'm going to do some searching on google. there must be an answer.
    thank for the help

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [2005] vs addin doesn't work

    Hey,

    This article doesn't mention your problem specifically, but maybe worth trying the first three suggestions to see if it helps any.

    Gary

  15. #15

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] vs addin doesn't work

    i tried running vb as administrator + running it in xp sp2 compatibility mode. neither worked.

  16. #16

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] vs addin doesn't work

    i asked on MSDN Extensibility forum, got some help there.
    this works:

    vb Code:
    1. Imports System
    2. Imports System.Collections.Generic
    3. Imports System.Windows.Forms
    4. Imports Microsoft.VisualStudio.CommandBars
    5. Imports Extensibility
    6. Imports EnvDTE
    7. Imports EnvDTE80
    8.  
    9. Public Class Connect
    10.     Implements Extensibility.IDTExtensibility2
    11.     Implements IDTCommandTarget
    12.     Private Const MY_COMMAND_NAME As String = "MyCommand"
    13.     Private applicationObject As EnvDTE.DTE
    14.     Private addInInstance As EnvDTE.AddIn
    15.     Private myCodeWindowCommandBarControl As CommandBarControl
    16.     Public Sub OnConnection(ByVal application As Object, ByVal connectMode _
    17.        As Extensibility.ext_ConnectMode, ByVal addInInst As Object, _
    18.        ByRef custom As System.Array) _
    19.        Implements Extensibility.IDTExtensibility2.OnConnection
    20.         Try
    21.             applicationObject = CType(application, EnvDTE.DTE)
    22.             addInInstance = CType(addInInst, EnvDTE.AddIn)
    23.             Select Case connectMode
    24.                 Case ext_ConnectMode.ext_cm_Startup
    25.                     ' The add-in was marked to load on startup
    26.                     ' Do nothing at this point because the IDE may not be fully initialized
    27.                     ' VS will call OnStartupComplete when ready
    28.                 Case ext_ConnectMode.ext_cm_AfterStartup
    29.                     ' The add-in was loaded after startup
    30.                     ' Initialize it in the same way that when is loaded on startup calling manually this method:
    31.                     OnStartupComplete(Nothing)
    32.             End Select
    33.         Catch e As System.Exception
    34.             System.Windows.Forms.MessageBox.Show(e.ToString)
    35.         End Try
    36.     End Sub
    37.     Public Sub OnStartupComplete(ByRef custom As System.Array) _
    38.        Implements Extensibility.IDTExtensibility2.OnStartupComplete
    39.         Dim myCommand As Command = Nothing
    40.         Dim codeCommandBar As CommandBar
    41.         Dim commandBars As CommandBars
    42.         Try
    43.  
    44.             ' Try to retrieve the command, just in case it was already created
    45.             Try
    46.                 myCommand = applicationObject.Commands.Item(addInInstance.ProgID & _
    47.                    "." & MY_COMMAND_NAME)
    48.             Catch
    49.             End Try
    50.             ' Add the command if it does not exist
    51.             If myCommand Is Nothing Then
    52.                 myCommand = applicationObject.Commands.AddNamedCommand(addInInstance, _
    53.                    MY_COMMAND_NAME, "Paste VBCode", "Paste formatted VBCode", True, _
    54.                    22, Nothing, vsCommandStatus.vsCommandStatusSupported Or _
    55.                    vsCommandStatus.vsCommandStatusEnabled)
    56.             End If
    57.             ' Retrieve the collection of commandbars
    58.             commandBars = DirectCast(applicationObject.CommandBars, CommandBars)
    59.             codeCommandBar = commandBars.Item("Code Window")
    60.             myCodeWindowCommandBarControl = DirectCast(myCommand.AddControl(codeCommandBar, _
    61.                FindPasteOnCommandBar("Code Window") + 1), CommandBarControl)
    62.             myCodeWindowCommandBarControl.Caption = "Paste VBCode"
    63.         Catch e As System.Exception
    64.             System.Windows.Forms.MessageBox.Show(e.ToString)
    65.         End Try
    66.     End Sub
    67.     Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, _
    68.        ByRef custom As System.Array) _
    69.        Implements Extensibility.IDTExtensibility2.OnDisconnection
    70.         Try
    71.             If Not (myCodeWindowCommandBarControl Is Nothing) Then
    72.                 myCodeWindowCommandBarControl.Delete()
    73.             End If
    74.         Catch e As System.Exception
    75.             System.Windows.Forms.MessageBox.Show(e.ToString)
    76.         End Try
    77.     End Sub
    78.     Public Sub OnBeginShutdown(ByRef custom As System.Array) _
    79.        Implements Extensibility.IDTExtensibility2.OnBeginShutdown
    80.     End Sub
    81.     Public Sub OnAddInsUpdate(ByRef custom As System.Array) _
    82.         Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
    83.     End Sub
    84.     Public Sub Exec(ByVal cmdName As String, _
    85.        ByVal executeOption As vsCommandExecOption, _
    86.        ByRef varIn As Object, ByRef varOut As Object, ByRef handled As Boolean) _
    87.        Implements IDTCommandTarget.Exec
    88.         handled = False
    89.         If (executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault) Then
    90.             If cmdName = addInInstance.ProgID & "." & MY_COMMAND_NAME Then
    91.                 handled = True
    92.                 Dim rtb As New RichTextBox
    93.                 rtb.Paste()
    94.                 rtb.SelectAll()
    95.                 rtb.Cut()
    96.                 Me.applicationObject.ActiveDocument.Selection.Insert(Clipboard.GetText)
    97.             End If
    98.         End If
    99.     End Sub
    100.     Public Sub QueryStatus(ByVal cmdName As String, _
    101.        ByVal neededText As vsCommandStatusTextWanted, _
    102.        ByRef statusOption As vsCommandStatus, ByRef commandText As Object) _
    103.        Implements IDTCommandTarget.QueryStatus
    104.         If neededText = vsCommandStatusTextWanted.vsCommandStatusTextWantedNone Then
    105.             If cmdName = addInInstance.ProgID & "." & MY_COMMAND_NAME Then
    106.                 statusOption = CType(vsCommandStatus.vsCommandStatusEnabled + _
    107.                    vsCommandStatus.vsCommandStatusSupported, vsCommandStatus)
    108.             Else
    109.                 statusOption = vsCommandStatus.vsCommandStatusUnsupported
    110.             End If
    111.         End If
    112.     End Sub
    113.     Private Function FindPasteOnCommandBar(ByVal commandBarName As String) As Integer
    114.  
    115.         Dim commandBars As CommandBars = Me.applicationObject.CommandBars
    116.         For index As Integer = 1 To commandBars(commandBarName).Controls.Count
    117.             If commandBars(commandBarName).Controls(index).Caption.Replace("&", "") = "Paste" Then
    118.                 Return index
    119.             End If
    120.         Next
    121.  
    122.     End Function
    123. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width