Click here to Skip to main content
15,441,508 members
Articles / Visual Studio
Posted 8 Jan 2012

Tagged as


9 bookmarked

Create Visual Studio forms for items you frequently use

Rate me:
Please Sign up or sign in to vote.
4.90/5 (10 votes)
8 Jan 2012CPOL2 min read
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!


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

Written By
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

GeneralReason for my vote of 3 Use one of VS templates - Dialog win... Pin
Vinden14-Jan-12 9:52
MemberVinden14-Jan-12 9:52 
GeneralReason for my vote of 5 Nice reminder of VS.Net's features. Pin
VBGuruWantabe11-Jan-12 6:57
MemberVBGuruWantabe11-Jan-12 6:57 
GeneralReason for my vote of 4 Concise, clear instructions for a ve... Pin
wkiess0110-Jan-12 11:09
Memberwkiess0110-Jan-12 11:09 
GeneralReason for my vote of 5 Quite helpful. How about changing th... Pin
us47119-Jan-12 12:03
Memberus47119-Jan-12 12:03 
GeneralReason for my vote of 5 nice post Pin
htxuankhoa8-Jan-12 2:34
Memberhtxuankhoa8-Jan-12 2:34 
QuestionGreat Post Pin
Eric Castellon8-Jan-12 2:12
MemberEric Castellon8-Jan-12 2:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.