A Better Way to Copy Forms – Templates

I just found a better way to maintain a master form without going all the way to inheritance. You can make a template out of your form, and then create new forms based on this template. The new forms are completely independent, without any link to the template the created them, so you can modify them further as much as you want. The process requires a little more work up front, but creating the forms does not.

Creating the Template

Start by looking for the Export Template… menu entry. It seems its location can vary, depending on Visual Studio version. Look in the File and Project menus. The wizard will start, asking you for the template type. This would be an Item Template.

In the next page of the wizard, select the form from which you want to make a template. The form itself will remain untouched, so don’t worry about that. The next page will ask which references to include. Any I select gives me a warning, and things worked fine when I didn’t select any. We then come to the final page of the wizard.

Here’s where you give the template a name. It automatically defaults to the name of the project, but as you type a different name, the file name in Output location will automatically change. I think Output location can vary with Visual Studio installation, so don’t worry if yours is different.

Regarding the two checkboxes, you want to automatically import the template into Visual Studio. This lets you create forms from the template more easily. The second checkbox just determines whether File Explorer opens upon creating the template. Anyway, I called mine GenericForm and pressed Finish.

Creating Forms from the Template

It couldn’t be easier. Go to add a new item or a new form, and you will see GenericForm as one of the options.

Deleting a Form Template

I was somewhat alarmed at first, when I right-clicked on GenericForm, expecting to get a pop-up menu with the option to delete, and nothing happened. To change the template, you just need to overwrite it, but what if you want to delete it? Maybe you created it by mistake. Or maybe you just don’t need it anymore and don’t want it cluttering the interface. Well, it turns out there’s no feature within Visual Studio that lets you do this, but it’s easy enough to do manually.

Search your Documents folder for the template by name. You know it’s a zip file, so you can narrow the search down. As you can see, Visual Studio puts it in two locations. The one that counts, that is, the one that appears when you add a new item or form, is the one in ItemTemplates. If you delete this one, you won’t see it anymore. I’m guessing you can keep the other copy in case you ever want to restore it.

Ok, so now that we’ve created a form template from Form1, let’s delete Form1. We’ll use the template after this.

Leave a Reply

Your email address will not be published. Required fields are marked *