1

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'?

Pete Hilde
  • 659
  • 1
  • 10
  • 24
  • 2
    Make the Main Form as simple as possible (`BasicMainForm`), UI wise. Then have Main Form inherit from `BasicMainForm`, and have the other forms inherit from `BasicMainForm`. – mjwills Jul 23 '18 at 06:31
  • You only need one form and a bunch of classes to perform the processing. I would make one class contain the data and pass the data between the classes and the form – jdweng Jul 23 '18 at 06:35
  • It sounds like you need to separate the UI from the logic and use some form of composition rather than inheritance. – Enigmativity Jul 23 '18 at 10:28

1 Answers1

2

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")].

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • When I do it like this, all the child classes that inherit from my base class are interpreted as normal classes without a designer. As a result I cannot load the Designer anymore. And this is what I actually still want to accomplish. Because if my base class is marked with `[System.ComponentModel.DesignerCategory("")]`, all derived classes will have that same property too... – Pete Hilde Jul 23 '18 at 10:11
  • That attribute is just there to prevent the form being open in designer. For forms which you want to be editable in designer, add you can add `[System.ComponentModel.DesignerCategory("Form")]`. If for any reason you decided to ignore the attribute, it's OK. Just don't open base form in designer. You have nothing to do with designer of that form. If you open it in designer, `InitializeComponent` will be added automatically. – Reza Aghaei Jul 23 '18 at 10:15
  • Thanks for editing, that's what was missing. Problem now: I get an exception when I try to open the derived forms. It says **The designer could not be shown for this file because none of the classes within it can be designed.** My base class _MainTest_ derives from `System.Windows.Forms.Form`, my child classes derive from _MainTest_. What am I doing wrong? – Pete Hilde Jul 23 '18 at 10:28
  • I just closed all designer forms and then cleaned and rebuild the solution and I have no problem in editing child forms in designer. (VS2017-Version 15.7.5) – Reza Aghaei Jul 23 '18 at 10:43
  • Don't forget that using that attribute is not compulsory. It's just for helping to prevent accidental design of the base form. If it's making trouble for you - while I have no problem using it - you can decide to remove it. – Reza Aghaei Jul 23 '18 at 10:45
  • I also did what you did (clean & rebuild) but I still get that exception. What I didn't mention was that I marked my base class as abstract to provide some methods that will be overridden in the child classes. However, I don't want to get too complicated. Now, I removed all occurences of `[System.ComponentModel.DesignerCategory("")]` respectively `[System.ComponentModel.DesignerCategory("Form")]` out of my base respectively child class. I also removed `abstract`from my base class. But I still get that same exception. – Pete Hilde Jul 23 '18 at 11:41
  • [MSDN](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/43fzdd42(v=vs.100)) tells me that the first class in the specific file is not designable.. – Pete Hilde Jul 23 '18 at 11:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176566/discussion-between-pete-hilde-and-reza-aghaei). – Pete Hilde Jul 23 '18 at 12:08
  • The MSDN entry I believe is not relevant to this problem, unless you really have put the base form class in the same file that your child form is located. – Reza Aghaei Jul 24 '18 at 08:13
  • Anyway, as I told you, I just did what I shared in the comments, and clean and rebuild the solution and all things works as expected. You can also try closing VS and clean bin/obj, then reopen VS and then rebuild. Also don't miss the first paragraph of the answer which is a better way to go (IMO). – Reza Aghaei Jul 24 '18 at 08:15
  • To find more information about how designer works, take a look at [this post](https://stackoverflow.com/a/32299687/3110834). – Reza Aghaei Jul 24 '18 at 19:28