Add-In code generator

MarMan

Member
Joined
Jan 4, 2010
Messages
17
Programming Experience
10+
Hello,

I was wondering if anyone has done any add-in development with VB.NET 2008. I am writing a code generator and am having a difficult time trying to figure out how to add code to a module or class or whatever. I can add a module with this:
VB.NET:
DTE.ItemOperations.AddNewItem("Common Items\Code\Class", "DataAccess.vb")

That returns a ProjectItem object, but I do not know how to access the text. It gives a reference to a Document object, but according to the help, a TextDocument object is required for text manipulation. But I can't find one.

If I use the macro recorder, I get code like this:
VB.NET:
DTE.ActiveDocument.Selection.NewLine()

Which won't compile because I have Option Strict on. I shouldn't have to have anything late bound.

I also looked at many pages here: http://msdn.microsoft.com/en-us/library/envdte.projectitem.document%28v=VS.80%29.aspx but got no where.

I would like to add code to modules and classes along with controls and code to forms. Any one know any good sites/links or can provide me with a couple of lines of code to demonstrate this? I am having no luck.
 
I figured out how to add code. Now I have to figure out how to add controls to a form. Here is an example on how to add code:

VB.NET:
        Const mconDataAccessFileName = "DataAccess.vb"
        Dim VB As DTE
        Dim EP As EditPoint
        Dim TD As TextDocument
        Dim obj As Object


            VB.ItemOperations.AddNewItem("Common Items\Code\Class", mconDataAccessFileName)

            'Following function produces some code
            strDACode = CreateCode()

            obj = VB.ActiveDocument.Object("TextDocument")
            TD = CType(obj, TextDocument)
            EP = TD.CreateEditPoint
            EP.LineDown(2)'Moves down past the 'Public Class...' statement
            EP.Insert(strDACode)

Recording a macro helps a bit, but the code often needs to be tweaked to run at all. If anyone figures out how to add controls before I do this would be a good place to put that code :)
 
Here's code that will add a textbox and a button to a form from an Add-In.

VB.NET:
        Dim strFormName As String
        Dim host As IDesignerHost
        Dim button As IComponent
        Dim component As IComponent
        Dim parent As PropertyDescriptor


        DTE.ItemOperations.AddNewItem("Common Items\Windows Forms\Windows Form", strFormName & ".vb")
        ' Get IDesignerHost, the root of the forms designer object model
        host = CType(DTE.ActiveWindow.Object, IDesignerHost)

        button = host.CreateComponent(host.GetType("System.Windows.Forms.Button,System.Windows.Forms"))
        parent = TypeDescriptor.GetProperties(button)("Parent")
        parent.SetValue(button, host.RootComponent)

        component = host.CreateComponent(host.GetType("System.Windows.Forms.TextBox,System.Windows.Forms"))
        parent = TypeDescriptor.GetProperties(component)("Parent")
        parent.SetValue(component, host.RootComponent)
 
Back
Top