views:

1080

answers:

6

Like Master pages in ASP.NET, do we have any similar concept for Windows Form application. So that I dont have to repeat the same portion of form (Header and footer) many times through out the application.

+1  A: 

Not really the same as master pages, but you can create a control (e.g based on UserControl) with header and footer that all your other forms can inherit (or construct) as needed.

Ed Guiness
+5  A: 

Yes.

What you have to do is to create your "Master" form, add in all the UI you want, the default buttons, etc.

For things like buttons, I typically create the methods that handle the Click events as "Virtual" so that I can implement them in the actual form that I'm then building.

Once the "Master" form is built, what you will need to do is to Right-Click on the project and select "New Item". Once you have done that, if you're in VS.Net 2008, you can click on the "Windows Forms" categories, and then select "Inherited Form".

Once you have done that, you will be asked to inherit from which form in the project. Simply select the "master" form, and you should be set.

Richard B
+5  A: 

Create the main form as a Form with header and footer, but leave the middle empty.

Implement "inner pages" as UserControl with a common interface, and change them as you need it. (Header, Contentpanel and Footer are Windows.Form.Panel).

-------------------------------
| Header                      |
-------------------------------         ______________
|                             |         |            |
|         ContentPanel        |   <---- | MonkeyEdit |
|                             |         |____________|
-------------------------------
| Footer                      |
-------------------------------

And implement content as

public class MonkeyEdit : UserControl, IContent
{

}

Implementing an interface it's usefull but not neccessary. After that, based on events/configuration, just load MonkeyEdit into the ContentPanel with a Dock.Fill. You can inhert the "master form" too, as you see here in an another post.

boj
A: 

You can achieve something similar with inheritance; you create some parent form with controls that are repeated and then use this parent for every custom form you need.

Mr. Brownstone
A: 

Add a class the inherits from Form, add the headers and footers(in code) and then let all new forms inherit from this class.

geoff
A: 

Master page has capability of specifying the content area where pages will be embedded. So deriving a form, say Form B from Form A and getting features of parent form doesn't prove the point. What Boj has mentioned makes more sense, where we are using Panels.