Reputation: 709
I'm currently working on a multi-form program for testing Data Acquisition Processors.
The software itself offers functions to perform certain tests on the DAP. Currently, each test has its own form, as well as associated methods and attributes. However, it is currently very redundant, since all tests need to use the same methods in some cases, but these are defined equally in each form.
Because of this problem I logically consider the concept of inheritance as a solution. However, the problem is that when I have a main form and I derive all other test forms from it, they are also visually derived from the main form. However, I want to stop exactly that. I only need the derived methods and attributes, not the visual derivation.
Is there a concept that does exactly that? Can I prevent the "InitializeComponents()" method from being called? Or is it already sufficient if I mark my main form as 'abstract'?
Upvotes: 1
Views: 286
Reputation: 125217
When you derive from a form, the constructor of the base form will run in your child form. So basically the visual inheritance is result of the code that you have in constructor of the base form.
Before deciding to continue deriving from a base form, as an option, you may want to consider creating business logic classes as container for your methods. You can simply have inheritance chain between business logic classes if you need. Also instead of deriving from form, you can inject instance of business logic classes into the forms.
But for any reason, if you prefer to use a form as base class, you can create a base form class without having any UI logic in the base form, to do so, it's enough to create such class:
[System.ComponentModel.DesignerCategory("")]
public class BaseForm : System.Windows.Forms.Form
{
//Don't define InitializeComponent
//Add methods here
}
In above class, no InitializeComponent
is defined and also by using [DesignerCategory("")]
the designer of the class has been disabled, so you cannot open it in designer. Then you can simply derive from that base form.
Note: Using the attribute is not compulsory. It's just there to prevent the base form getting open. If are sure you will not open that form in designer, you can remove it. If you keep it, designer of child forms will be disabled by default. To enable child forms designer, it's enough to decorate them with [System.ComponentModel.DesignerCategory("Form")]
.
Upvotes: 2