No reference to project item created through AddFromTemplate() returned
Asked Answered
A

1

5

In a Visual Studio Add-In that successfully creates a form, but gives me no reference back to the EnvDTE's prjItem instance.

Here's the piece of code that adds the form to the project:

string templatePath = solution.GetProjectItemTemplate("Form.zip", "csproj");
ProjectItem prjItem = project.ProjectItems.AddFromTemplate(templatePath, "myForm.cs");

Obs.: 'solution' is an EnvDTE80.Solution2 object.

Of cource I can get the reference by other ways, like proj.ProjectItems.Item([index]) (doing a loop and checking for names), but that's not how I want to do it, and I need this reference in orther to add controls to this form.

Am I doing something wrong?

Abbreviation answered 3/3, 2011 at 17:36 Comment(1)
Just wrote a related question: #5185870Abbreviation
A
8

Just found a comment on MSDN:

AddFromTemplate always returns a NULL value

At one time, this was true. But with later versions of Visual Studio, which included the ability to add multiple items from a single template, the return value for this method could not return multiple items. So it now returns a NULL value in all instances. This is due to the contraint that the COM signature for this particular method cannot be changed without breaking a lot of code already in use.

Consequently, if you need the ProjectItem interface of the item just added via the AddFromTemplate call, you can either iterate through the ProjectItems collection, or you can create a ProjectItemsEvents.ItemAdded event just before calling AddFromTemplate, and store away the ProjectItem passed to your OnItemAdded handler.

http://msdn.microsoft.com/en-us/library/envdte.projectitems.addfromtemplate(v=vs.80).aspx#1

Accessory answered 3/3, 2011 at 17:52 Comment(2)
This creates a new project in na existing solution. I want to create a new project Item (in my case, a Windows Form) in an existing project..Abbreviation
This surely helps a lot! Well, in the end I'll do what I was avoiding to, but at least I already know it works.. Thanks a lot!Abbreviation

© 2022 - 2024 — McMap. All rights reserved.