1

I have questions about inheritance of UserControl in a WinForm project.

I have several specific panel which are different page of my application. But these specific panel have some common code repeated inside each of these panels.

So I want to code their common behaviour in a StdPanel class inherited from UserControl and then each specific pages will inherit from the StdPanel.

A specificPanel:

    public partial class SpecificPanel1 : StdPanel
    { 
        public SpecificPanel1 () : base()
        {
            InitializeComponent();
        }
    }

The StdPanel:

    public partial class StdPanel: UserControl
    {  
        public StdPanel()
        {
            InitializeComponent();//Keeping this line or not?
        }

        private void StdPanel_VisibleChanged(object sender, EventArgs e)
        {
          if (Visible)
          {
            //do stuff...
          }
        }
    }

My questions:

My questions: I want to do things when StdPanel_VisibleChanged, I did a lot of testing and I understand that InitializeComponent() is necessary to make the StdPanel_VisibleChanged event work. At first, I wanted to remove InitializeComponent() from StdPanel because the GUI is done in a SpecificPanel1 , and I thought it was not necessary.

So does SpecificPanel1 have two GUI?

How are these two GUI nested? Are they present on the screen but one hides the second?

Thank you

Aurel
  • 45
  • 3
  • Do you want to use WIndows-Forms Designer ? Then keep the line. It's special to this class, it's not inherited or something, you can check what's going on in there. If you have created the event-handler through the Properties-window you better keep it as it is. What interfaces are you talking of ? There are no interfaces in your code. – Holger Jan 14 '20 at 15:46
  • @Holger Guessing he means GUI's rather than C# interfaces – Timmeh Jan 14 '20 at 15:46
  • Yes, I mean GUI instead interface. Thank you. – Aurel Jan 14 '20 at 15:53
  • You can use `InitializeComponent()` to redefine the layout of inherited components. Usually, your base class' Controls have an access modifier set to `private`. In this case, the `InitializeComponent()` method of derived classes cannot redefine the layout of the base class. You can set the modifier to `protected`/`protected internal`, so you can redefine the layout of these Controls in derived classes. This new behavior, specific to the derived class, is then defined in the `InitializeComponent()`, which is called when the derived class is initialized. – Jimi Jan 14 '20 at 16:06
  • The `InitializeComponent()` method of the derived class can have *local* additions to the base class *template*, to redefine/specialize the graphic interface. There's more to it, though, related to the Form Designer requirements, since the base class and the derived classes all have their own designer. – Jimi Jan 14 '20 at 16:10
  • As a side note, when you build a base class, you don't subscribe to events, you override the method: you should have `protected override void OnVisibleChanged(EventArgs e) { base.OnVisibleChanged(e); (...) }` in the base class. Also, see the notes [here](https://stackoverflow.com/a/55760332/7444103) about the sequence of events when base/derived classes are initialized. – Jimi Jan 14 '20 at 16:23
  • Thanks a lot Jimi. You understood my question. It is clearer for me now. – Aurel Jan 14 '20 at 19:33

1 Answers1

0

The word "partial" splits a class over two files. There are no two classes and no two GUI's. Just two files of source code for one class only.

This helps to separate generated code, from your own manual written code.

The designer.cs you can look at, but better don't modify it (unless you know what you are doing).

Holger
  • 2,446
  • 1
  • 14
  • 13
  • Even if you know what you're doing, it should be regarded as readonly as the designer UI can and will override your changes. – Timmeh Jan 14 '20 at 15:52
  • Maybe my question is not clear enough: there one InitializeComponent() in SpecificPanel1 and one InitializeComponent() in StdPanel. SpecificPanel1 inherit from StdPanel. Is there two GUI? Each InitializeComponent() is generating a GUI, isn't it? – Aurel Jan 14 '20 at 15:59
  • @Timmeh That's only true if designer is open. Designer will read this file to show the visual UI. As long es he can read it, everything is fine. – Holger Jan 14 '20 at 16:02
  • @Aurel You just should not do that. Don't inherit from Designer created UserControls. Rather put one control into the other. (like you put a label in a control). Answer is still the same: Don't delete it, if you want to use Designer. And look into "InitilizeComponent" to learn what is going on in there. – Holger Jan 14 '20 at 16:05