Click here to Skip to main content
15,892,298 members
Articles / Web Development / HTML

Building a Shared Code Library as a VS.NET Plug-in

Rate me:
Please Sign up or sign in to vote.
4.00/5 (7 votes)
30 Mar 20055 min read 89.2K   594   41  
A plug-in for VS.NET that stores code snippets in a database. From the plug-in you can add code, search the database for code snippets. Also includes examples on how to integrate with the IDE as a plug-in.
'Copyright (c) Microsoft Corporation.  All rights reserved.

Imports EnvDTE
Imports System.Runtime.InteropServices

<ComVisible(False)> _
Public Class CmdBrowserFrm
    Inherits System.Windows.Forms.Form
    Dim m_dteInstance As EnvDTE.DTE
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents closeButton As System.Windows.Forms.Button
    Dim searchNode As System.Windows.Forms.TreeNode

#Region " Windows Form Designer generated code "

    Public Sub New(ByVal dteInstance As EnvDTE.DTE)
        MyBase.New()

        m_dteInstance = dteInstance

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents infoTreeView As System.Windows.Forms.TreeView
    Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
    Friend WithEvents informationButton As System.Windows.Forms.Button
    Friend WithEvents searchTextBox As System.Windows.Forms.TextBox
    Friend WithEvents searchButton As System.Windows.Forms.Button

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.infoTreeView = New System.Windows.Forms.TreeView()
        Me.informationButton = New System.Windows.Forms.Button()
        Me.searchTextBox = New System.Windows.Forms.TextBox()
        Me.searchButton = New System.Windows.Forms.Button()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.closeButton = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'infoTreeView
        '
        Me.infoTreeView.HideSelection = False
        Me.infoTreeView.ImageIndex = -1
        Me.infoTreeView.Name = "infoTreeView"
        Me.infoTreeView.SelectedImageIndex = -1
        Me.infoTreeView.Size = New System.Drawing.Size(424, 256)
        Me.infoTreeView.TabIndex = 0
        '
        'informationButton
        '
        Me.informationButton.Location = New System.Drawing.Point(126, 264)
        Me.informationButton.Name = "informationButton"
        Me.informationButton.TabIndex = 1
        Me.informationButton.Text = "&Information"
        '
        'searchTextBox
        '
        Me.searchTextBox.Location = New System.Drawing.Point(48, 312)
        Me.searchTextBox.Name = "searchTextBox"
        Me.searchTextBox.Size = New System.Drawing.Size(288, 20)
        Me.searchTextBox.TabIndex = 4
        Me.searchTextBox.Text = ""
        '
        'searchButton
        '
        Me.searchButton.Location = New System.Drawing.Point(344, 312)
        Me.searchButton.Name = "searchButton"
        Me.searchButton.TabIndex = 5
        Me.searchButton.Text = "&Search"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(0, 312)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(48, 23)
        Me.Label1.TabIndex = 3
        Me.Label1.Text = "S&earch:"
        '
        'GroupBox1
        '
        Me.GroupBox1.Location = New System.Drawing.Point(0, 288)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(440, 8)
        Me.GroupBox1.TabIndex = 5
        Me.GroupBox1.TabStop = False
        '
        'closeButton
        '
        Me.closeButton.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.closeButton.Location = New System.Drawing.Point(223, 264)
        Me.closeButton.Name = "closeButton"
        Me.closeButton.TabIndex = 2
        Me.closeButton.Text = "Close"
        '
        'CmdBrowserFrm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(424, 342)
        Me.ControlBox = False
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.closeButton, Me.GroupBox1, Me.Label1, Me.searchButton, Me.searchTextBox, Me.informationButton, Me.infoTreeView})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "CmdBrowserFrm"
        Me.ShowInTaskbar = False
        Me.Text = "Command Browser"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Structure commandBarControlData
        Dim commandName As String
    End Structure

    Private Sub WalkCommandBars(ByVal currNode As System.Windows.Forms.TreeNode, ByVal cmdBar As Microsoft.Office.Core.CommandBar)
        Dim cmdBarCtl As Microsoft.Office.Core.CommandBarControl
        Dim addedNode As System.Windows.Forms.TreeNode
        addedNode = currNode.Nodes.Add(cmdBar.Name)

        For Each cmdBarCtl In cmdBar.Controls
            If (cmdBarCtl.Type = Microsoft.Office.Core.MsoControlType.msoControlButton) Then
                Dim tmpCtl As System.Windows.Forms.TreeNode
                Dim cbcData As commandBarControlData
                Dim guidCommand As String
                Dim idCommand As Integer
                tmpCtl = addedNode.Nodes.Add(cmdBarCtl.Caption)
                Try
                    m_dteInstance.Commands.CommandInfo(cmdBarCtl, guidCommand, idCommand)
                    cbcData.commandName = m_dteInstance.Commands.Item(guidCommand, idCommand).Name
                    tmpCtl.Tag = cbcData
                Catch ex As System.Exception
                End Try
            ElseIf (cmdBarCtl.Type = Microsoft.Office.Core.MsoControlType.msoControlPopup) Then
                Dim cmdBarPopup As Microsoft.Office.Core.CommandBarPopup

                cmdBarPopup = CType(cmdBarCtl, Microsoft.Office.Core.CommandBarPopup)
                WalkCommandBars(addedNode, cmdBarPopup.CommandBar)
            End If
        Next
    End Sub

    Private Sub CmdBrowserFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cmd As EnvDTE.Command
        Dim commandsNode As System.Windows.Forms.TreeNode
        Dim toolBars As System.Windows.Forms.TreeNode

        commandsNode = infoTreeView.Nodes.Add("Commands")
        toolBars = infoTreeView.Nodes.Add("Tool Bars")

        For Each cmd In m_dteInstance.Commands
            If (cmd.Name <> Nothing) Then
                commandsNode.Nodes.Add(cmd.Name)
            End If
        Next

        Dim cmdBar As Microsoft.Office.Core.CommandBar
        For Each cmdBar In m_dteInstance.CommandBars
            WalkCommandBars(toolBars, cmdBar)
        Next
    End Sub

    Private Sub informationButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles informationButton.Click
        Dim currentNode As System.Windows.Forms.TreeNode
        Dim tempNode As System.Windows.Forms.TreeNode
        currentNode = infoTreeView.SelectedNode
        If ((currentNode Is Nothing) Or (currentNode.Parent Is Nothing)) Then
            MsgBox("Cannot get information for the currently selected node", MsgBoxStyle.Information, "Command Browser")
        Else
            tempNode = currentNode
            While (Not (tempNode.Parent.Parent Is Nothing))
                tempNode = tempNode.Parent
            End While
            If (tempNode.Parent.Text = "Commands") Then
                Dim cmdInfoFrm As CommandInformationFrm
                cmdInfoFrm = New CommandInformationFrm(m_dteInstance, infoTreeView.SelectedNode.Text)
                cmdInfoFrm.ShowDialog(Me)
            ElseIf (tempNode.Parent.Text = "Tool Bars") Then
                If (currentNode.Tag Is Nothing) Then
                    MsgBox("Cannot get information for the currently selected node", MsgBoxStyle.Information, "Command Browser")
                Else
                    Dim cbcData As commandBarControlData
                    Dim cmdInfoFrm As CommandInformationFrm
                    cbcData = CType(currentNode.Tag, commandBarControlData)
                    cmdInfoFrm = New CommandInformationFrm(m_dteInstance, cbcData.commandName)
                    cmdInfoFrm.ShowDialog(Me)
                End If
            End If
        End If
    End Sub

    Private Function FindNextNode(ByVal searchNode As System.Windows.Forms.TreeNode) As System.Windows.Forms.TreeNode
        'Strategy for finding nodes: 
        ' 1) Start at the current node, and check if there are any sub items.
        ' 2) If there are, then get the first one
        ' 3) If there are not, then get the sibling of this node
        ' 4) If there are no siblings, get the parent, and find the sibling of that item
        ' 5) If the parent does not have siblings, find the parent of the parent and repeat step 4
        ' 6) If nothing is left, then return with Nothing.
        While (Not (searchNode Is Nothing))
            Dim tnCln As System.Collections.ICollection
            tnCln = searchNode.Nodes

            If (tnCln.Count = 0) Then
                If (searchNode.NextNode Is Nothing) Then
                    Dim tmp As System.Windows.Forms.TreeNode
                    tmp = searchNode.Parent.NextNode
                    If (tmp Is Nothing) Then
                        tmp = searchNode.Parent
                        If (tmp Is Nothing) Then
                            Return Nothing
                        Else
                            While (tmp.NextNode Is Nothing)
                                tmp = tmp.Parent
                                If (tmp Is Nothing) Then
                                    Return Nothing
                                End If
                            End While
                        End If
                        Return tmp.NextNode
                    End If
                    Return tmp
                Else
                    Return searchNode.NextNode
                End If
            Else
                Return searchNode.Nodes.Item(0)
            End If
        End While
    End Function

    Private Sub ItemNotFound()
        MsgBox("Search Complete", MsgBoxStyle.Information, "Command Browser")
        searchButton.Enabled = True
        searchButton.Text = "&Search"
    End Sub

    Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click
        Dim tempSearchNode As System.Windows.Forms.TreeNode
        searchButton.Enabled = False
        searchButton.Text = "Searching..."

        'If a search has not started, prime the current node:
        If (searchNode Is Nothing) Then
            searchNode = infoTreeView.Nodes.Item(0)
        Else
            searchNode = FindNextNode(searchNode)

            'Reached the end of the available items:
            If (searchNode Is Nothing) Then
                ItemNotFound()
                Return
            End If
        End If

        'Loop until either something is found, or nothing is left to be found:
        While (True)
            'When getting information from the command bars, wherever there is an '_' on a character, 
            ' it will appear with an '&' in the name. Search with and without the '&':
            Dim strNode As String = searchNode.Text.Replace("&", "")
            If (searchNode.Text.IndexOf(searchTextBox.Text) <> -1) Or (strNode.IndexOf(searchTextBox.Text) <> -1) Then
                searchNode.Expand()
                searchNode.EnsureVisible()
                infoTreeView.SelectedNode = searchNode
                Exit While
            End If

            searchNode = FindNextNode(searchNode)

            'Reached the end of the available items:
            If (searchNode Is Nothing) Then
                ItemNotFound()
                Return
            End If
        End While

        searchButton.Text = "Find &Next"
        searchButton.Enabled = True
    End Sub

    Private Sub searchTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles searchTextBox.TextChanged
        searchButton.Text = "&Search"
        searchNode = Nothing
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Thailand Thailand
Spent my whole life developing, having worked in C++, Delphi, ASP, then finally settling down to working solely in ASP.Net. Also a forex day trader.

Comments and Discussions