locked
Item template OpenInEditor attribute. RRS feed

  • Question

  • I have an item template where I want OpenInEditor=true when the item is added explicitly by the user via the Add New Item dialog. But, I want OpenInEditor=false when the item is added programmatically via ProjectItems.AddFromTemplate.  The core issue is that our designers add files and we don't want the focus to change away from the designer when we programmatically add those project items.

    Thanks in advance.


    Kirk Fertitta

    Thursday, March 12, 2015 6:28 PM

Answers

  • Hi Kirk,

    Good question. Simple solution would be to have a 2nd vstemplate (just set it's hidden flag so it doesn't show up in the user's Add Item dialog), and use that one in your code to programmatically add the same item.

    Just an 'off the cuff' suggestion :-)

    Sincerely,


    Ed Dore

    Wednesday, March 18, 2015 9:39 PM

All replies

  • Hi Kirk,

    Yes, by default this method will automatically open the added project item and activate it. But you could  then activate the original document or find the opened document and close it programmatically. For example, in the following code, I just create a test.cs file, then activate the original document.

    DTE dte = GetService(typeof(SDTE)) as DTE;
    
                Document doc = dte.ActiveDocument;
    
                Project project = dte.Solution.Projects.Item(1);
    
                string fileName = "test.cs";
    
                string template = (dte.Solution as Solution2).GetProjectItemTemplate("Class", "CSharp");
    
                ProjectItem pi=project.ProjectItems.AddFromTemplate(template, fileName);
    
                doc.Activate();


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 13, 2015 9:08 AM
  • Thank you for taking the time to reply.  Yes, I do know I can programmatically activate or close, but it's actually the case that when I programmatically call AddFromTemplate that I do *not* want it open.  If I programmatically close the document, then there will likely at least be a "flash" and my designer will still lose focus, so it's not quite "smooth".

    Kirk Fertitta

    Friday, March 13, 2015 1:14 PM
  • Hi Kirk,

    Good question. Simple solution would be to have a 2nd vstemplate (just set it's hidden flag so it doesn't show up in the user's Add Item dialog), and use that one in your code to programmatically add the same item.

    Just an 'off the cuff' suggestion :-)

    Sincerely,


    Ed Dore

    Wednesday, March 18, 2015 9:39 PM
  • Hi Ed,

    Good to hear from you, and thanks for taking the time to answer.  Yeah, I think this is a reasonable approach.  I think what I lose is that we lose the ability for users to customize that template.  Well, we don't lose it, but we have to document that they'd need to customize the template twice -- the visible one and the hidden one. It's livable.  I just wanted to make sure that there wasn't a magic programmatic switch somewhere before I charged down that path.

    Thanks again for your answer.


    Kirk Fertitta

    Thursday, March 19, 2015 12:21 PM