Greg, your comment about inheritence is very interesting - could it be that inheritenceis simply the best approach anyway. Now that I think aboutdoes creating a new template just give a head start on things but doesn't help with future maintenance, i.e if I use MyTemplate01 as the base for my forms and, in six months time, find that I missed a facility that I need in all forms then I have to go back and recreate all my forms using the an updated version of MyTemplate01.
Alternatively, if all forms inherited from MyForm01 then I simply change MyForm01 and recompile - is this right?
Peter, I'm thinking that using both is the best, as Ben said. I didn't get the template thing until reading this post though.
Let me give you a couple of examples of some of the things done in my base form to demonstrate the usefulness of inheritance:
- It can be configured to set the title of the form based on the value of a field in a BO. Currently I'm setting the BO and the field name as properties in the base form, but it recently occurred to me that since my base form inherits from the SF standard form, I could have just used the BO collection...duh. Then I'd only need to set the field name. The base form has code to react to the navigated event and handles setting the title of the form. I.e. if it is an employee form, the title will be '[employee name] - Employee Form' were [employee name] is replaced by the name of the employee.
- I've included a SF Gradient header and a .NET tablelayout control just under it to show the client and project context of the form (most of my forms are within the context of client/project). The base form has a property so the project ID can be set. The base form handles the Load event of the form and sets the header up, looking up the client and project name using the project ID.
In both cases, if I find a bug or want to change the behavior I have one place to fix it. If I find myself copy/pasting the same code between forms, I start thinking about how to use inheritance to reuse the copy/pasted code. As an example, when I first coded the loading of the project/client name, I was looking it up always. Then I realized that in most cases I could cache this info, so I just changed the code to use a cache and a property to force a lookup. About 80% of the forms in my app used this base form and they all got fixed instantly.
I really like using inheritance because it allows very iterative programming. As I figure out that I have repetitive code in a form, I refactor, pulling out the repetitive code and put it my base form. I'm actually about to refactor my base form into two base forms, one that has very common code (like the title setting bit using a field form a BO) and my project base form (which would have the custom header for projects). Thus I'd have a specific project form inherit from the project base form which inherits from the general base form which inherits from the SF standard form which inherits from the .net form! This can get a bit confusing (because the features implemented in a form are spread all over the place..see note at end), but believe me, the first time you find a bug or want to change/add a feature that is in the general base form and see the change reflected instantly in ALL forms inheriting from it...oh baby
However, to use this base form, I have to manually open the designer file and change the inheritance. This is where the template would be really nice. When I get a moment, I'm going to give it a try!
NOTE:
I've found that using xml comments and keeping them updated is very helpful as those comments get pushed into the object browser. This allows me to more quickly remember how I've done things. I can see the inheritance, which property/methods are in which of the inheritated forms etc.