Visual Studio Developer Center > Visual Studio Forums > Visual Studio Extensibility > VSPackage: How to create a custom virtual node like References node
Ask a questionAsk a question
 

Proposed AnswerVSPackage: How to create a custom virtual node like References node

  • Thursday, June 25, 2009 6:24 PMKenny Chinn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,
         I have a C# flavored project and I want to add some custom nodes to my project where the only options when you right click on them is to add or remove a custom project item that I have already created, so I want it to be like the References node in that it cannot be deleted renamed and it only has 2 context menu options. Any idea or links on how to do something like this would be greatly appreciated.

    Thanks

    Kenny

All Replies

  • Thursday, June 25, 2009 11:19 PMReed Shilts Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    This is actually easier than you would think (and I have bunches of custom containers and node types)...
    a) Create a container class (like a folder) which inherits from HierarchyNode.
    b) Create an item class (like a file) which also inherits from HierarchyNode.

    To make it impossible to rename from explorer, override the "SetEditLabel" method and throw NotImplementedException (yes, it's a rather silly technique, but that's the only thing that works).  I also override "CanDeleteItem" and return false.

    For the container class - override the GetProperty() method and return true for "VSHPROPID_Expandable".
    For the item class - don't return true for the ...Expandable (of course).

    Take a look at the FileNode and FolderNode classes in the MPF ProjectBase and you'll see how easy this ends up being.

    Hope this helps,
    Reed SHilts

    • Proposed As Answer byReed Shilts Thursday, June 25, 2009 11:19 PM
    •  
  • Wednesday, July 01, 2009 5:40 PMKenny Chinn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for the repsonse but we are not using MPF so we cannot use the HierachyNode class as it uses ProjectNode. Is there any way to do this sort of thing using a Flavored C# project?


    Thanks
    Kenny
  • Thursday, July 02, 2009 1:51 AMNancy ShaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    Hi Kenny,

    Based on my  understanding, you can use Project Template to add a new ProjectNode as Reference Node in treeview. You can export a C# project template by File>Export Template... and  then customize your project template, please refer to following code snippet about how to add a new folder note(named NewFolder1) in project template:

    <TemplateContent>
        <Project TargetFileName="ConsoleApplication.csproj" File="ConsoleApplication.csproj" ReplaceParameters="true">
          <Folder Name="NewFolder1" TargetFolderName="NewFolder1" />
          <ProjectItem ReplaceParameters="true" TargetFileName="Program.cs">Program.cs</ProjectItem>
          <Folder Name="Properties" TargetFolderName="Properties">
            <ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
          </Folder>
        </Project>
      </TemplateContent>
    
    For more information, please see : Create Reusable Project And Item Templates For Your Development Team.

    If I misunderstood you, or you have any questions, please let me know.

    Best Regards,
    Nancy
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Thursday, July 02, 2009 2:14 PMKenny Chinn Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Nancy,
       Thanks for the response, but I think you misunderstood me. I need to create a custom folder in which all the user can do is add or remove a custom project item I have created. I want it to be sort of like the References folder, except you can add my custom project items instead of references. Does that make senese? If you have any questions for me please let me know. 

    Thanks
    Kenny