Click here to Skip to main content
11,565,352 members (38,982 online)
Click here to Skip to main content

How to derive from a parent form

, 12 Oct 2007 CPOL 46.5K 48
Rate this:
Please Sign up or sign in to vote.
Create a parent form from which other forms can be derived.

Introduction

Once upon a time, there was an intelligent, talented and handsome (lets not deny the facts) developer. One fine Microsoft enabled day said developer was handed a task to completed by the developer - he read the specs smiled and was happy.

This one particular task (which our hero had been entrusted) had many forms/dialog boxes for the user to interact with. Many of these forms were fairly similar and had common traits which were shared – that is branding, buttons control position and functionality.

Breaking the forms down the developer noted that there were many common elements that they all shared and decided that the best way forward was to use a "parent" form. From this parent many of the other forms could inherit from.

A Code Project First!

To try out something totally new for The Code Project I made an instructional video - Chris has spoken with the hampsters and included the special tags and we're all happy to bring to you the article/video pair.

You can reinforce the tutorial with the video...or vice versa. Cheers CodeProject hampsters!

Basic Procedure

The procedure to make use of this methodology is straightforward. We need to make a form which will serve as the "parent form". This form should have the common properties and controls (and functionality) which are used in those forms who will inherit from them.

What do I mean?

If all our forms have a Cancel button, an OK button, background colour then we can set one up as the base from which all the others will get their setup from.

Follow these steps:

  1. Make a base form


  2. Add the OK and cancel buttons and add a picture box control (to hold our branding)



  3. Set the image of the picture box and resize if necessary, background colour and so on.



  4. Add in any common functionality code which you will need.

  5. Looks well lets just say it looks like Chris after too much errr 'Coca Cola'

  6. Build the application - if you don't you will not be able to inherit from the form

  7. Now its time to add in a new form which will inherit from the above parent


  8. When the "Add New Item" dialog box appears – select "Inherited Form"



  9. The next dialog which appears is where you get to choose which form is the new form will be based on – if you did not build your project earlier then the parent form will not appear here.

    Select your parent form and click OK.

  10. A new form will be added to your project. It will look exactly the same as your original parent form. But will have a different form name – in our case it looks like the following

    Note that it even has the same title and the wee arrows on the controls – this indicates that you have an inherited form and the properties cannot be changed yet. If you attempt to modified the properties you will find that you will be unable to. However you will be able to add new controls and code to the form..

    Cool, looking better – save and build the app and you're good to go.

Private – Protected.

Now let's say that you want to keep the OK and the cancel buttons and the branding and so on, but you want to be able to move them about on each form.

To do this you need to :
  1. Go to the designer file for the original form – the parent form In our example its Form1.Designer.cs.

  2. Find the declarations for the controls you want to be able to move – by default they'll all be set to "private".

  1. Change the "private" to "protected" save your work!

  1. Rebuild your application - you must do this before attempting to modify the inherited form

  2. Return to your derived form and you will now be able to move your controls about (yay).

Finally

Once you have rebuilt the solution again (and every time a new form is added)– you will be able to use more forms which are derived from an original. (Or even derive from one of the derived)

Points of Interest

A big "take-home message" from this is that with some forethought you can save yourself so much work its not funny. And that's quite cool I think – as did our hero and he completed his project in record time to adoration and adulation of his peers.

Herein ends the tale.

License

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

Share

About the Author

bryce
Software Developer (Senior)
Australia Australia
No Biography provided

You may also be interested in...

Comments and Discussions

 
Question[My vote of 1] My Vote of One Pin
dl4gbe8-Oct-11 7:51
memberdl4gbe8-Oct-11 7:51 
AnswerRe: [My vote of 1] My Vote of One Pin
bryce9-Oct-11 13:26
memberbryce9-Oct-11 13:26 
GeneralRe: [My vote of 1] My Vote of One Pin
dl4gbe10-Oct-11 1:16
memberdl4gbe10-Oct-11 1:16 
GeneralRe: [My vote of 1] My Vote of One Pin
bryce10-Oct-11 13:20
memberbryce10-Oct-11 13:20 
GeneralMy vote of 5 Pin
thatraja29-May-11 16:53
mvpthatraja29-May-11 16:53 
GeneralNot All controls can be set to protected... Pin
nionios2725-May-08 4:19
membernionios2725-May-08 4:19 
GeneralQuestion. Pin
topcatalpha16-Oct-07 20:53
membertopcatalpha16-Oct-07 20:53 
GeneralRe: Question. Pin
bryce18-Oct-07 14:21
memberbryce18-Oct-07 14:21 
GeneralRe: Question. Pin
topcatalpha18-Oct-07 21:03
membertopcatalpha18-Oct-07 21:03 
QuestionHow to do Visual Inheritence in WPF? Pin
Probeer Das15-Oct-07 5:24
memberProbeer Das15-Oct-07 5:24 
AnswerRe: How to do Visual Inheritence in WPF? Pin
bryce15-Oct-07 14:50
memberbryce15-Oct-07 14:50 
QuestionInherited control actions (button click etc.) Pin
Chaitanya Joshi13-Oct-07 22:56
memberChaitanya Joshi13-Oct-07 22:56 
GeneralRe: Inherited control actions (button click etc.) Pin
Chaitanya Joshi13-Oct-07 23:01
memberChaitanya Joshi13-Oct-07 23:01 
GeneralRe: Inherited control actions (button click etc.) Pin
bryce14-Oct-07 0:54
memberbryce14-Oct-07 0:54 
GeneralRe: Inherited control actions (button click etc.) Pin
Chaitanya Joshi14-Oct-07 20:57
memberChaitanya Joshi14-Oct-07 20:57 
GeneralRe: Inherited control actions (button click etc.) Pin
Chaitanya Joshi15-Oct-07 22:03
memberChaitanya Joshi15-Oct-07 22:03 
AnswerRe: Inherited control actions (button click etc.) Pin
Daniel Ruehmer15-Oct-07 20:41
memberDaniel Ruehmer15-Oct-07 20:41 
GeneralRe: Inherited control actions (button click etc.) Pin
Chaitanya Joshi15-Oct-07 21:58
memberChaitanya Joshi15-Oct-07 21:58 
GeneralRe: Inherited control actions (button click etc.) Pin
Daniel Ruehmer17-Oct-07 1:33
memberDaniel Ruehmer17-Oct-07 1:33 
GeneralEditing designer code Pin
OrlandoCurioso13-Oct-07 1:52
memberOrlandoCurioso13-Oct-07 1:52 
GeneralRe: Editing designer code Pin
bryce13-Oct-07 2:01
memberbryce13-Oct-07 2:01 
always good to have an alternative - thank you Smile | :)

Bryce


---
To paraphrase Fred Dagg - the views expressed in this post are bloody good ones.
--

Publitor, making Pubmed easy.
http://www.sohocode.com/publitor

Our kids books :The Snot Goblin, and Book 2 - the Snotgoblin and Fluff


GeneralRe: Editing designer code Pin
sherifffruitfly13-Oct-07 18:17
membersherifffruitfly13-Oct-07 18:17 
GeneralRe: Editing designer code Pin
TobiasP15-Oct-07 0:47
memberTobiasP15-Oct-07 0:47 
General5 for doing the video Pin
sameeraperera12-Oct-07 5:00
membersameeraperera12-Oct-07 5:00 
GeneralRe: 5 for doing the video Pin
Initri12-Oct-07 5:32
memberInitri12-Oct-07 5:32 

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 | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 12 Oct 2007
Article Copyright 2007 by bryce
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid