Click here to Skip to main content
Click here to Skip to main content

Tagged as

Create Visual Studio forms for items you frequently use

, 8 Jan 2012
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

OriginalGriff
CEO
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?
Follow on   Google+

Comments and Discussions

 
GeneralReason for my vote of 3 Use one of VS templates - Dialog win... PinmemberVinden14-Jan-12 9:52 
GeneralReason for my vote of 5 Nice reminder of VS.Net's features. PinmemberVBGuruWantabe11-Jan-12 6:57 
GeneralReason for my vote of 4 Concise, clear instructions for a ve... Pinmemberwkiess0110-Jan-12 11:09 
GeneralReason for my vote of 5 Quite helpful. How about changing th... Pinmemberus47119-Jan-12 12:03 
GeneralReason for my vote of 5 nice post Pinmemberhtxuankhoa8-Jan-12 2:34 
QuestionGreat Post PinmemberEric Castellon8-Jan-12 2:12 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 8 Jan 2012
Article Copyright 2012 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid