Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Visual-Studio

Create Visual Studio forms for items you frequently use

4.90/5 (10 votes)
8 Jan 2012CPOL2 min read 27.4K  
Quite often, when I create a new form, the first thing I need to add is an OK and a Cancel button. Recently, I realized that I could create templates for these in Visual Studio and save some time.
I don't know why I never thought of it before, but it is really easy to do.

Step 1: Create your blank
Create a new Windows Form by right clicking your project and selecting "Add...Windows Form".
You can leave the name at the default value, as you will delete it at the end of the process, and the name is irrelevant to the template.
Add two buttons, one of the left of the screen, and then one on the right. (If you add these in the wrong order you will need to change the tab order or the user could get confused).
Move the right hand one into the bottom right hand corner of the form (so that the blue lines appear to the right and bottom of the button).
Change its name to "butCancel" (or whatever your normal name would be).
Change the Text property to "Cancel"
Change the DialogResult property to "Cancel"

Move the left hand button into position to the left of the Cancel button - again, the lines will tell you when it is correct.
Change it's name to "butOK" (or whatever your normal name would be).
Change the Text property to "OK"
Change the DialogResult property to "OK"

Highlight both buttons, by clicking on one, then holding down CTRL and clicking on the other.
Change the Anchor property from "Top" and "Left" to "Bottom" and "Right".

Highlight the form.
Change the Text property to "OK Cancel" or similar.
Change the AcceptButton property to "butOK".
Change the CancelButton property to "butCancel".
Change the MinimumSize property to "196, 0".

Step 2: Save as a template.
On the File menu, select "Export Template..."
In the resulting dialog box, select the "Item template" radio button - the default is "Project template" which exports a whole lot more than you want!
Select the appropriate project (the one you added the blank OKCancel form to), and press "Next >".
Select the blank OKCancel form, and press "Next >"
Do not add any references, unless you need them in all your forms - Press "Next >".
Give the template a sensible name and description, and press "Finish".

Done! You can now add a blank OK/Cancel form to any future projects.
You can delete that blank from your project now.

Using this I have added two "blank" forms - OKCancel and InputBox, simply because I generate forms based on these far too often!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)