views:

59

answers:

3

In web programming you have one or more master pages and some content pages that in which you add some content to the placeholders or access elements of the master. My question is how to handle windows forms that kinda have the same format, let's say header, content and footer. The footer is always the same, the header may slightly differ and the content will always differ(some times will be a grid view , some times some drop downs, etc) If you derive one form from some other form i saw that you cannot access the master form elements. So, should i create only user controls and make every form separately, without inheritance, using user controls?

+2  A: 

Use inheritance and make sure that the elements from the master form your are trying to access are declared as Protected (or public).

This way the elements will be visible from the child forms.

Ando
+1  A: 

You should create a class that has a private form as a variable, with a couple of labels, just anything you need. Then you can take two approaches:

  1. Expose a few properties, like SaveFileDialog, that represent the things you want customizable. In the setter, you set the labels, pictureboxes, etc. to the correct values. Then, you create a show method with no parameters and show the form in it.

  2. Expose just a show method, like in MessageBox, and add all the customizable parts as parameters.

Don't subclass Form, because that will just be a mess; people can customize stuff you don't want them to.

Jouke van der Maas
Optional: make them static.
Jouke van der Maas
A: 

The cheap way to do it is to have your content controls overlay each other in the UI. There's only one form with all the controls it can ever show, but only the appropriate controls are visible at runtime.

You could also use a tab control for, say, different screens of a wizard. It depends on how complex your UI is.

Beth