Click here to Skip to main content
15,896,912 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.
Imports System.Windows.Forms
Imports System.Diagnostics
Imports EnvDTE 'DTE.Windows


Public Class ucCodeLibrary
    Inherits System.Windows.Forms.UserControl
    Private win As Window '= ApplicationObject.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
    ' Create handles to the Output window and its panes.
    Private OW As OutputWindow '= win.Object
    Private OWp As OutputWindowPane
    Private pApplicationObject As EnvDTE.DTE
    Private aCodeDBFunctions As New clsCodeDBFunctions
    Dim aClsWinAuth As New clsWinAuthentication

    Public Property ApplicationObject() As EnvDTE.DTE
        Get
            Return pApplicationObject
        End Get
        Set(ByVal Value As EnvDTE.DTE)
            pApplicationObject = Value
        End Set
    End Property

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

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

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl 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

    '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.
    Friend WithEvents pnlTop As System.Windows.Forms.Panel
    Friend WithEvents tvMain As System.Windows.Forms.TreeView
    Friend WithEvents btnRefresh As System.Windows.Forms.Button
    Friend WithEvents btnHelp As System.Windows.Forms.Button
    Friend WithEvents ctEntry As System.Windows.Forms.ContextMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem
    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
    Friend WithEvents pnlMain As System.Windows.Forms.Panel
    Friend WithEvents btnUpdateDB As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(ucCodeLibrary))
        Me.tvMain = New System.Windows.Forms.TreeView
        Me.ctEntry = New System.Windows.Forms.ContextMenu
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.MenuItem5 = New System.Windows.Forms.MenuItem
        Me.MenuItem4 = New System.Windows.Forms.MenuItem
        Me.MenuItem9 = New System.Windows.Forms.MenuItem
        Me.MenuItem2 = New System.Windows.Forms.MenuItem
        Me.MenuItem6 = New System.Windows.Forms.MenuItem
        Me.MenuItem3 = New System.Windows.Forms.MenuItem
        Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
        Me.pnlTop = New System.Windows.Forms.Panel
        Me.btnUpdateDB = New System.Windows.Forms.Button
        Me.btnHelp = New System.Windows.Forms.Button
        Me.btnRefresh = New System.Windows.Forms.Button
        Me.pnlMain = New System.Windows.Forms.Panel
        Me.pnlTop.SuspendLayout()
        Me.pnlMain.SuspendLayout()
        Me.SuspendLayout()
        '
        'tvMain
        '
        Me.tvMain.CausesValidation = False
        Me.tvMain.ContextMenu = Me.ctEntry
        Me.tvMain.Dock = System.Windows.Forms.DockStyle.Fill
        Me.tvMain.ImageIndex = 4
        Me.tvMain.ImageList = Me.ImageList1
        Me.tvMain.Location = New System.Drawing.Point(0, 0)
        Me.tvMain.Name = "tvMain"
        Me.tvMain.Size = New System.Drawing.Size(184, 400)
        Me.tvMain.TabIndex = 1
        '
        'ctEntry
        '
        Me.ctEntry.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem5, Me.MenuItem4, Me.MenuItem2, Me.MenuItem6, Me.MenuItem3})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.Text = "View..."
        '
        'MenuItem5
        '
        Me.MenuItem5.Index = 1
        Me.MenuItem5.Text = "-"
        '
        'MenuItem4
        '
        Me.MenuItem4.Index = 2
        Me.MenuItem4.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem9})
        Me.MenuItem4.Text = "Add"
        '
        'MenuItem9
        '
        Me.MenuItem9.Index = 0
        Me.MenuItem9.Text = "Entry"
        '
        'MenuItem2
        '
        Me.MenuItem2.Index = 3
        Me.MenuItem2.Text = "Insert"
        '
        'MenuItem6
        '
        Me.MenuItem6.Index = 4
        Me.MenuItem6.Text = "-"
        '
        'MenuItem3
        '
        Me.MenuItem3.Index = 5
        Me.MenuItem3.Text = "Delete"
        '
        'ImageList1
        '
        Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
        Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
        '
        'pnlTop
        '
        Me.pnlTop.Controls.Add(Me.btnUpdateDB)
        Me.pnlTop.Controls.Add(Me.btnHelp)
        Me.pnlTop.Controls.Add(Me.btnRefresh)
        Me.pnlTop.Dock = System.Windows.Forms.DockStyle.Top
        Me.pnlTop.Location = New System.Drawing.Point(0, 0)
        Me.pnlTop.Name = "pnlTop"
        Me.pnlTop.Size = New System.Drawing.Size(184, 32)
        Me.pnlTop.TabIndex = 2
        '
        'btnUpdateDB
        '
        Me.btnUpdateDB.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.btnUpdateDB.ImageAlign = System.Drawing.ContentAlignment.BottomCenter
        Me.btnUpdateDB.Location = New System.Drawing.Point(65, 4)
        Me.btnUpdateDB.Name = "btnUpdateDB"
        Me.btnUpdateDB.Size = New System.Drawing.Size(80, 24)
        Me.btnUpdateDB.TabIndex = 2
        Me.btnUpdateDB.Text = "Up to date"
        '
        'btnHelp
        '
        Me.btnHelp.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.btnHelp.Image = CType(resources.GetObject("btnHelp.Image"), System.Drawing.Image)
        Me.btnHelp.ImageAlign = System.Drawing.ContentAlignment.BottomCenter
        Me.btnHelp.Location = New System.Drawing.Point(34, 4)
        Me.btnHelp.Name = "btnHelp"
        Me.btnHelp.Size = New System.Drawing.Size(24, 24)
        Me.btnHelp.TabIndex = 1
        '
        'btnRefresh
        '
        Me.btnRefresh.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.btnRefresh.Image = CType(resources.GetObject("btnRefresh.Image"), System.Drawing.Image)
        Me.btnRefresh.Location = New System.Drawing.Point(8, 4)
        Me.btnRefresh.Name = "btnRefresh"
        Me.btnRefresh.Size = New System.Drawing.Size(24, 24)
        Me.btnRefresh.TabIndex = 0
        '
        'pnlMain
        '
        Me.pnlMain.Controls.Add(Me.tvMain)
        Me.pnlMain.Dock = System.Windows.Forms.DockStyle.Fill
        Me.pnlMain.Location = New System.Drawing.Point(0, 32)
        Me.pnlMain.Name = "pnlMain"
        Me.pnlMain.Size = New System.Drawing.Size(184, 400)
        Me.pnlMain.TabIndex = 3
        '
        'usCodeLibrary
        '
        Me.Controls.Add(Me.pnlMain)
        Me.Controls.Add(Me.pnlTop)
        Me.Name = "usCodeLibrary"
        Me.Size = New System.Drawing.Size(184, 432)
        Me.pnlTop.ResumeLayout(False)
        Me.pnlMain.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region "Initialising the control"


    Private Sub usCodeLibrary_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitialiseControl()
    End Sub

    Private Sub InitialiseControl()
        'LoadTree()
        'AuthenticateUser()
        'AddOutputWindow()
    End Sub

    Private Sub AuthenticateUser()
        If Not aClsWinAuth.AuthenticateUser Then
            aClsWinAuth.AddUserToDB()
        Else
            WriteToOutputPane("User " & aClsWinAuth.FirstName & " was authenticated") 'MessageBox.Show("Hello " & aClsWinAuth.WinDomain & "/" & aClsWinAuth.WinUser)
        End If
    End Sub

    Private Sub LoadTree()
        aCodeDBFunctions.LoadTree(tvMain)
    End Sub
#End Region

#Region "Unfinished Code"
    Private Sub InsertCode(ByVal anodetag As clsNodeTag)
        ' this is where it calls the applicationobject
        MessageBox.Show("Entry " & anodetag.CodeText & "to be inserted")
    End Sub

    Private Sub handleViewClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MessageBox.Show("Handle the View clicks here")

        ' what menu was clicked

        ' what is the selected node?
    End Sub
#End Region

#Region "Control Events"

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        LoadTree()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateDB.Click
        '  MessageBox.Show(Application.ProductVersion)
        btnUpdateDB.Text = "Up to Date"
    End Sub

    Private Sub tvMain_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tvMain.DoubleClick
        Dim selNode As System.Windows.Forms.TreeNode
        Dim aNodeTag As clsNodeTag
        selNode = tvMain.SelectedNode()
        aNodeTag = selNode.Tag()
        InsertCode(aNodeTag)
    End Sub

    Private Sub tvMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvMain.MouseDown
        ' need to modify the context menu based on whether its a 

        '  MessageBox.Show(tvMain.SelectedNode.Tag)
        ' do the context menu change here
        Dim selNode As System.Windows.Forms.TreeNode
        Dim aNodeTag As clsNodeTag
        selNode = tvMain.GetNodeAt(e.X, e.Y)
        tvMain.SelectedNode = selNode

        If Not selNode Is Nothing Then
            aNodeTag = selNode.Tag()

            If aNodeTag.IsCode Then
                BuildContextMenu(True)
            Else
                '    tvMain.ContextMenu = ctCategory
                BuildContextMenu(False)
            End If
        Else : ctEntry.MenuItems.Clear()
        End If
    End Sub
#End Region

#Region "Handle all the menu events"

    Private Sub BuildContextMenu(ByVal ForCode As Boolean)
        Dim newMenuItem As MenuItem
        Dim newMenuItem2 As MenuItem
        Dim newMenuItem3 As MenuItem
        ctEntry.MenuItems.Clear()
        If ForCode Then
            newMenuItem = ctEntry.MenuItems.Add("View...")
            AddHandler newMenuItem.Click, AddressOf handleViewClick
        Else
            newMenuItem2 = ctEntry.MenuItems.Add("Add Code...")
            AddHandler newMenuItem2.Click, AddressOf handleAddEntryClick

            newMenuItem3 = ctEntry.MenuItems.Add("Add Category...")
            AddHandler newMenuItem3.Click, AddressOf handleAddCategoryClick
        End If

    End Sub

    Private Sub handleAddCategoryClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' need to insert a category here - need to know the category name and icon
        Dim CatName As String ' = InputBox("What would you like the category to be called?", "title", "My Category")
        Dim IconID As Integer

        Dim afrmAddCategory As New frmAddCategory
        afrmAddCategory.LoadImagestoList(ImageList1)
        'afrmAddCategory.ImageList1 = 
        Dim aDialogResult As DialogResult
        aDialogResult = afrmAddCategory.ShowDialog()

        If aDialogResult = DialogResult.OK Then
            CatName = afrmAddCategory.CategoryName
            IconID = afrmAddCategory.IconID
            ' throw up a form with the required information on there
            Dim selNode As System.Windows.Forms.TreeNode
            Dim addNode As New TreeNode
            Dim aNodeTag As New clsNodeTag
            selNode = tvMain.SelectedNode()

            aNodeTag.CodeText = CatName
            aNodeTag.IsCode = False
            aNodeTag.EntryID = aCodeDBFunctions.GetMaxCodeEntryID + 1 ' have to get the latest entryid here

            addNode.Tag = aNodeTag
            addNode.Text = CatName
            addNode.SelectedImageIndex = IconID
            addNode.ImageIndex = IconID

            ' need to fill out the node tag information here as well
            selNode.Nodes.Add(addNode)
            selNode.Expand()
            tvMain.SelectedNode = addNode

            'save the item in the database
            Dim ParentNodeID As Integer
            Dim refNodeTag As clsNodeTag

            If selNode Is Nothing Then
                ParentNodeID = 0
            Else
                refNodeTag = selNode.Tag
                ParentNodeID = refNodeTag.EntryID
            End If

            aCodeDBFunctions.InsertToDBCodeCategory(CatName, IconID, ParentNodeID, Nothing)

            ' now persist to the database - for now we update a flag which will indicate it needs saving
            WriteToOutputPane("Category added : " & CatName)
        End If
    End Sub

    Private Sub handleAddEntryClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim afrmAddCodeEntry As New frmAddCodeEntry
        Dim selNode As TreeNode
        ' set up the user name
        afrmAddCodeEntry.UserName = aClsWinAuth.FirstName
        afrmAddCodeEntry.UserID = aClsWinAuth.UserID

        ' set up the category
        selNode = tvMain.SelectedNode()
        afrmAddCodeEntry.CategoryName = selNode.Text
        afrmAddCodeEntry.ApplicationObject = ApplicationObject
        ' aDialogResult = 

        If afrmAddCodeEntry.ShowDialog() = DialogResult.OK Then
            If afrmAddCodeEntry.CodeSaved Then
                ' we need to update the tree with the new entry
                Dim CodeCaption As String = afrmAddCodeEntry.CodeCaption

                ' throw up a form with the required information on there
                Dim addNode As New TreeNode
                Dim aNodeTag As New clsNodeTag

                aNodeTag.CodeText = afrmAddCodeEntry.CodeText
                aNodeTag.IsCode = True
                aNodeTag.EntryID = aCodeDBFunctions.GetMaxCodeEntryID + 1 ' have to get the latest entryid here

                addNode.Tag = aNodeTag
                addNode.Text = CodeCaption
                addNode.SelectedImageIndex = 4
                addNode.ImageIndex = 4

                ' need to fill out the node tag information here as well
                selNode.Nodes.Add(addNode)
                selNode.Expand()
                tvMain.SelectedNode = addNode

                ' now persist to the database - for now we update a flag which will indicate it needs saving


                'save the item in the database
                Dim ParentNodeID As Integer
                Dim refNodeTag As clsNodeTag

                If selNode Is Nothing Then
                    ParentNodeID = 0
                Else
                    refNodeTag = selNode.Tag
                    ParentNodeID = refNodeTag.EntryID
                End If

                aCodeDBFunctions.InsertToDBCodeCategory(CodeCaption, 4, ParentNodeID, afrmAddCodeEntry.CodeEntryID)
                WriteToOutputPane("Code Entry added : " & CodeCaption)
            Else
                WriteToOutputPane("error saving code")
            End If

        End If
    End Sub

#End Region

#Region "Helper to write to the output window"


    Sub AddOutputWindow()
        If Not ApplicationObject Is Nothing Then
            ' Create a tool window handle for the Output window.
            win = ApplicationObject.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
            ' Create handles to the Output window and its panes.
            OW = win.Object
            ' Add a new pane to the Output window.
            OWp = OW.OutputWindowPanes.Add("Code library")
            ' Add a line of text to the new pane.
        End If
    End Sub

    Private Sub WriteToOutputPane(ByVal Message As String)
        ' will eventually use the output pane
        If pApplicationObject Is Nothing Then
            System.Diagnostics.Debug.Write(Message)
        Else
            System.Diagnostics.Trace.Write(Message)
            OWp.OutputString(Message & vbLf)
        End If
    End Sub
#End Region

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