Home > database >  Generic control to have inherit methods in WinForm
Generic control to have inherit methods in WinForm

Time:07-11

I have to work on a project on which there are several Form which have 80% of the code the same. So I try to create a generic class to make inheritate all my Forms of the UserControl class (the basic one) and my own class. But .Net doesn't support multi classs inheritance. So I create a middle class to do the inheritance chain like I can see on the net but I think I miss another step. Each class is in a different file for information.

The problem is I can't open anymore the designer for my initial Forms, because "Visual Studio cannot open a designer for the file because the class within it does not inherit from a class that can be visually designed".

What I had at the beginning :

namespace i2SIMDCProduction
{
    public partial class MyForm1 : UserControl
    {
        public MyForm1(MyOwnClass myClass)
        {
            InitializeComponent();
            this.myClass = myClass;
        }
    }
}

namespace i2SIMDCProduction
{
    public partial class MyForm2 : UserControl
    {
        public MyForm2(MyOwnClass myClass)
        {
            InitializeComponent();
            this.myClass = myClass;
        }
    }
}

What I have now :

namespace i2SIMDCProduction
{
    public partial class MyForm1 : MyMiddleClass
    {
        public MyForm1(MyOwnClass myClass)
        {
            InitializeComponent();
            this.myClass = myClass;
        }
    }
}

namespace i2SIMDCProduction
{
    public partial class MyForm2 : MyMiddleClass
    {
        public MyForm2(MyOwnClass myClass)
        {
            InitializeComponent();
            this.myClass = myClass;
        }
    }
}
namespace i2SIMDCProduction
{
    public partial class MyMiddleClass : UserControl
    {

    }
    public void MethodForAllChild()
    {

    }
}

Thank you in advance for any kind of help. I tried different things already (create a third class at the top of the file of my Forms for example, create empty constructor, ...) but nothing which works for now. The more frustrating is it is compiling and working but only the designer is KO.

CodePudding user response:

If you want different forms to share the same visual controls on the screen then you set up inheritance between the forms.

Use the inherited form option in Visual Studio

fig1

For example, Form1 has a group box, with a label and two text boxes

scr1

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void CommonMethod()
        {
        }
    }

and Form2 inherits from From1 and adds a list box

scr2

    public partial class Form2 : Form1
    {
        public Form2()
        {
            InitializeComponent();
        }
    
        public void SpecificMethod()
        {
            base.CommonMethod();
        }

    }

As you can see the controls from Form1 show up on Form2 also with a little icon to indicate that they are inherited.


If instead you just need to share code (like business logic) and not visual controls, then you create a separate class to hold the code with a link to the parent form, and then each form should contain an instance of this class.

What you want to do is a Model-View-Controler setup, where the Model is only data-related classes, View is only UI code, and the controller goes between the two doing the heavy lifting with processing user inputs and updating values.

CodePudding user response:

The inheritance and designer in Windows Forms is a problem.

I have a Form with an splitter, two listboxes and some other controls. That form is used to translate (map) some items. You select one item at left, one at right and click button to match. They are the same item in different providers.

I have another provider that require some extra controls to do the translation. May be 90% or more of the code is the same, but I need some extra for this provider.

The options that I saw:

  • Add these extra controls (protected or public) to the Form, hidden by default and without use. In Form derived class, you use them. You haven't the designer in derived Form, but you don't need because controls are in base Form. The problem with this approach is that the designer part of inheritance of derived Form is in base Form. It's a nonsense. I don't recomend this option.

  • Don't use the designer in derived Form. Starting in the previous point, copy the designer code added for your derived Form into your derived Form and leave your base Form as at first stage, without nothing of derived Form. You don't use the designer but you can use it temporary, copy/paste and have a good inheritance... without the designer in derived Form. It's a good option if your derived Forms has few changes, few maintenance in the designer part.

  • You can "Add" some logic to your base Form to allow extensions. For example, below of the ListBox, I can add a Panel (hidden by default) and some methods like ShowLeftPanel/ShowRightPanel. By default, these panels aren't used, but in derived class I can add an UserControl in left panel and show it. And that UserControl show the properties that I need to show in the special provider. Add some virtual methods for listbox selection changed, to update the UserControl. In this way, your UserControl has designer and also the base Form. You only need add some "extension points" in your form (a Panel, a Splitter...) and give some methods to interact with this parts of the base Form. And this is ok with inheritance because is something generic, like Tag property in controls.

  • Related