locked
Add interface to newly created folder RRS feed

  • Question

  • Hi,

    I am creating an Item Template, have it almost working except for the following problem.

    I want to create a folder called "Interfaces" in my project and then add a file called Interfaces.vb to that folder, but I cannot seem to get this working.

    Here is a snippet of my simple code.

    Private Sub AddInterface(_dte As EnvDTE.DTE)
            Dim _project As EnvDTE.Project = Nothing
            For Each obj As Object In _dte.ActiveSolutionProjects
                _project = TryCast(obj, EnvDTE.Project)
                If _project Is Nothing Then Continue For
            Next
    
            Dim _folder As EnvDTE.ProjectItem
     	_folder = _project.ProjectItems.AddFolder("Interfaces")
            _folder.ProjectItems.AddFromTemplate("Code\Interface", "IReportSource.vb")
    End Sub

    I keep getting null reference exceptions, because _folder.ProjectItems is null. So how do I add a file to a folder?

    Gary

    Monday, September 16, 2013 3:04 PM

Answers

  • Hi,

    Actually the code above did not work as expected. so here is the working code. I decided against using the interface template as a starting point and used the CodeFile instead. reason for this was it seemed a lot easier use the code generator to create everything in the file instead of trying to append etc.

    I also decided against using the addfunction, addproperty methods cause I couldn't work out how to add an Event. Instead using the Editpoint functionality.

    _Dim _folder As EnvDTE.ProjectItem = _project.ProjectItems.AddFolder("Interfaces")
    Dim _itempath As String = _dte.Solution.GetProjectItemTemplate("CodeFile.zip", "vbproj")
    _folder.ProjectItems.AddFromTemplate(_itempath, "IReportSource.vb")
    _interfacefile = _folder.ProjectItems.Item("IReportSource.vb")
    
    Dim _filecodemodel As EnvDTE.FileCodeModel = _interfacefile.FileCodeModel
    _filecodemodel.AddImport("Microsoft.Reporting.WinForms")
    
    Dim _codenamespace As EnvDTE.CodeNamespace = _filecodemodel.AddNamespace("Interfaces")
    
    Dim _codeinterface As EnvDTE.CodeInterface = _codenamespace.AddInterface("IReportSource")
    _codeinterface.Access = EnvDTE.vsCMAccess.vsCMAccessPublic
    
    With _codeinterface
    
    Dim _textpoint As EnvDTE.EditPoint = .GetStartPoint(EnvDTE.vsCMPart.vsCMPartBody).CreateEditPoint
    _textpoint.Insert(String.Format("Property ReportParameters As IEnumerable(Of Microsoft.Reporting.WinForms.ReportParameter){0}", vbCr))
    _textpoint.Insert(String.Format("Property ReportDataSources As IEnumerable(Of Microsoft.Reporting.WinForms.ReportDataSource){0}", vbCr))
    _textpoint.Insert(String.Format("Property ReportEmbeddedResource As String{0}", vbCr))
    _textpoint.Insert(String.Format("Function Initialize(ByVal _localReport As Microsoft.Reporting.WinForms.LocalReport) As Boolean{0}", vbCr))
    _textpoint.Insert(String.Format("Event GenerateReport(ByVal sender As Object, e As EventArgs){0}", vbCr))
    _textpoint.Insert(String.Format("Property ReportTitle As String{0}", vbCr))
    _textpoint.Insert(String.Format("ReadOnly Property AcceptButton As IButtonControl{0}", vbCr))
    
    End With
    Gary
    • Marked as answer by garusher Tuesday, September 17, 2013 12:28 PM
    Tuesday, September 17, 2013 12:28 PM

All replies

  • Okay, I fixed the problem. needed the following.

            _folder = _project.ProjectItems.AddFolder("Interfaces")
            Dim _i As EnvDTE.ProjectItems = _folder.ProjectItems
            Dim _itempath As String = "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\VisualBasic\Code\1033\Interface.zip"
            _i.AddFromTemplate(_itempath, "IReportSource.vb")

    Monday, September 16, 2013 3:25 PM
  • Hi,

    I feel glad to hear that you have solved this problem timely.

    And thank you for sharing the solution with us here.

    Hope that we could often see your wise idea on others' questions.

    Best Regards,

    Jane.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, September 17, 2013 9:38 AM
  • Hi,

    Actually the code above did not work as expected. so here is the working code. I decided against using the interface template as a starting point and used the CodeFile instead. reason for this was it seemed a lot easier use the code generator to create everything in the file instead of trying to append etc.

    I also decided against using the addfunction, addproperty methods cause I couldn't work out how to add an Event. Instead using the Editpoint functionality.

    _Dim _folder As EnvDTE.ProjectItem = _project.ProjectItems.AddFolder("Interfaces")
    Dim _itempath As String = _dte.Solution.GetProjectItemTemplate("CodeFile.zip", "vbproj")
    _folder.ProjectItems.AddFromTemplate(_itempath, "IReportSource.vb")
    _interfacefile = _folder.ProjectItems.Item("IReportSource.vb")
    
    Dim _filecodemodel As EnvDTE.FileCodeModel = _interfacefile.FileCodeModel
    _filecodemodel.AddImport("Microsoft.Reporting.WinForms")
    
    Dim _codenamespace As EnvDTE.CodeNamespace = _filecodemodel.AddNamespace("Interfaces")
    
    Dim _codeinterface As EnvDTE.CodeInterface = _codenamespace.AddInterface("IReportSource")
    _codeinterface.Access = EnvDTE.vsCMAccess.vsCMAccessPublic
    
    With _codeinterface
    
    Dim _textpoint As EnvDTE.EditPoint = .GetStartPoint(EnvDTE.vsCMPart.vsCMPartBody).CreateEditPoint
    _textpoint.Insert(String.Format("Property ReportParameters As IEnumerable(Of Microsoft.Reporting.WinForms.ReportParameter){0}", vbCr))
    _textpoint.Insert(String.Format("Property ReportDataSources As IEnumerable(Of Microsoft.Reporting.WinForms.ReportDataSource){0}", vbCr))
    _textpoint.Insert(String.Format("Property ReportEmbeddedResource As String{0}", vbCr))
    _textpoint.Insert(String.Format("Function Initialize(ByVal _localReport As Microsoft.Reporting.WinForms.LocalReport) As Boolean{0}", vbCr))
    _textpoint.Insert(String.Format("Event GenerateReport(ByVal sender As Object, e As EventArgs){0}", vbCr))
    _textpoint.Insert(String.Format("Property ReportTitle As String{0}", vbCr))
    _textpoint.Insert(String.Format("ReadOnly Property AcceptButton As IButtonControl{0}", vbCr))
    
    End With
    Gary
    • Marked as answer by garusher Tuesday, September 17, 2013 12:28 PM
    Tuesday, September 17, 2013 12:28 PM