J Collins
J Collins

Reputation: 2170

Why does Visual Studio Designer instantiate a base class for controls?

I have a form which is based on another standard form I have (transparency supported form) which at runtime identifies itself by its own type. In the designer however, I have set up some message box outputs that show at design time it identifies on top of the designer control stack as the parent type.

Example code

Public Class SpecificForm
    Inherits GenericForm
    Implements IUsefulInterface
    ...
End Class

Public Class GenericForm
    Inherits System.Windows.Forms.Form
    ...
End Class

I have controls on the form that seek their parent to know how to draw correctly. At runtime it works fine since the specific type implements an interface carrying drawing information. Cycling through the parents yields

"Control1" As System.Windows.Forms.Control, parented by
"MainForm1" As SpecificForm, parented by
Nothing

as design time though cycling through the parents yields

"Control1" As System.Windows.Forms.Control, parented by
"MainForm1" As GenericForm, parented by
"" As System.Windows.Forms.Design.DesignerFrame+OverlayControl, parented by
"" As System.Windows.Forms.Design.DesignerFrame, parented by
"" As System.Windows.Forms.Control, parented by
Nothing

at runtime MainForm identifies as the SpecificForm type, at design time is is understandably hosted by the designer, but it now identifies as the GenericType.

Upvotes: 2

Views: 345

Answers (2)

Fernando Espinosa
Fernando Espinosa

Reputation: 4825

Also, to further document what you are seeing, you can do the following experiment:

Create a simple UserControl called FooUserControl which simply will be used to show a message box with the stack trace of the place where it was created:

using System;
using System.Windows.Forms;

namespace Test {
    public partial class FooUserControl : UserControl {
        public FooUserControl() {
            InitializeComponent();
            MessageBox.Show(Environment.StackTrace);
        }
    }
}

Build your project and now open any of your form designers, for example, to the desginer of a form called FooForm (it could be the designer of your SpecificForm or any other control you are authoring.) The moment that you drag and drop this FooUserControl on the designer surface you will see a message box saying something like;

enter image description here

And every time you re-open this designer you will see something like:

enter image description here

Whereas, every time you "run" you application you will see something like:

enter image description here

All this is saying, is that the Windows Forms designer is NOT even executing any such method called FooForm.InitializeComponent(). That method is executed only at runtime, since it's only at runtime when the constructor of your control being designed is executed, which in turn calls FooForm.InitializeComponent().

Believe it or not, what the designer is doing is essentially parsing statement by statement the lines in the FooForm.Designer.cs file (probably using CodeDOM).

Hope these explanations help.

Upvotes: 0

Fernando Espinosa
Fernando Espinosa

Reputation: 4825

I respect the fact that you're asking this question. But, and don't get me wrong, the question is somewhat a display of naïveté. You've observed a phenomenon that most people don't (Congrats you're approaching maturity as a WinForms programmer! -- I'm serious, not sarcastic..)

Now, I said that your question is a bit naïve, because it's like asking "why is the circle so round?". The next explanation might sound a bit redundant, but it's the only one:

At design-time, in the designer of the SpecificForm, you are NOT "running" an instance of SpecificForm, instead you are designing a prototype based on GenericForm and that is effectively the type of the control you are "running" (i.e., that is in a runtime context.)

I repeat, the designer of SpecificForm does not have an instance of SpecificForm "running", but an instance of GenericForm. No SpecificForm instance exists in the designer, because, well.. you are designing it!!!

I used this exact explanation to guide another user to understand what he was seeing: https://stackoverflow.com/a/12499053/1426433. But you noticed it by yourself.

Upvotes: 1

Related Questions