Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No reference to project item created through AddFromTemplate() returned

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?

like image 280
JohnDoe Avatar asked Mar 03 '11 17:03

JohnDoe


1 Answers

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

like image 129
thedev Avatar answered Nov 09 '22 18:11

thedev