Visual Studio Item Templates
Visual Studio Item Templates – In one of my previous posts (Programmer’s Survival Tips – #3) I looked at having your own code library or repository. While this is a really important aspect of programming, there are times when you would want to do a bit more than simply delve into your bag of code snippets. When I started out programming .NET applications, I frequently found myself wondering if there wasn’t an easier way to do things. Well to tell you the truth, there are so many Visual Studio Tips and Tricks out there, but I would like to focus on one in particular. Visual Studio Item Templates.
What Are Visual Studio Item Templates
Now for some of you, this will be old hat, but I bet that there are some of you who are not familiar with Item Templates in Visual Studio. Lets say for example that you would like to extend the error message screen that is displayed to the user in the catch portion of a try / catch block. So you go about designing your new screen and adding extra functionality, bells and whistles. Once your application is completed and QA’d, you know that it is ready to be rolled out to a live environment. Along comes the next project, and you would like to use the same extended error message screen as in the previous project. So now you copy the code and form from the previous project, and paste it into a new form in the new project. Well, there is an easier way – Visual Studio Item Templates.
How To Do It
If you have a DLL or a form or any item in your solution that you want to use over and over again, simply export it to a template. To do this, follow these steps:
- From the Visual Studio file menu, select Export Template
- On the Export Template Wizard screen, select the Item Template radio button and select the project in your solution to create an Item Template from
- The next step will list all the items in the selected project from the previous screen. Here you can select one or more Items to export
- Next, for the Item or Items you selected in the previous window, you now need to select the references you would like to include with them.
- On the last screen, it is wise to select a suitable Icon for the template. You also need to give the Item Template a suitable name as well as a description of the template. The output location for the Item Template is usually as follows:
C:\Users\[user name]\Documents\[Visual Studio version]\My Exported Templates
- You can select to import the template into Visual Studio automatically as well as display the explorer window on the output folder, but for this example, we’ll do it manually. Click Finish to export the Item Templates
Using The Exported Visual Studio Item Templates
The Exported Item Templates will be found in the My Exported Templates folder (at the path above). The Exported Item Templates should now be copied to the Item Templates Visual C# folder (If you use VB.NET, it will be in that folder) for use in Visual Studio. For C#, this folder is found here:
C:\Users\[user name]\Documents\[Visual Studio version]\Templates\ItemTemplates\Visual C#
This way, you can share Item Templates with other developers and use any Item Templates you receive from them. Just copy received templates to the Item Templates Visual C# folder.
Item Templates copied to your Visual C# folder will be usable within your Visual Studio projects. Right click on the project in Visual Studio’s Solution Explorer that you want to add the Item to, and from the context menu select Add > Add New Item. On the Add New Item screen, select the Visual C# Items node in the tree view on the left. You will see a section called My Templates in the Templates pane to the right. Exported Item Templates are visible here. You can now add any exported templates to your Visual Studio solution as needed.
This is an excellent way of building up a repository of complete code forms and DLL’s that you use often. A good tip is to make a habit of mining your completed projects for reusable components. The more work you do up front, the more Items you can export and reuse. This will effectively cut down on your development time. All this will make you a more efficient software developer.