I have a component I created in C# which had previously been using the default constructor, but now I would like it to have its parent form create the object (in the designer), by passing a reference to itself.
In other words, instead of the following in the designer.cs:
this.componentInstance = new MyControls.MyComponent();
I would like to instruct the form designer to create the following:
this.componentInstance = new MyControls.MyComponent(this);
Is it possible to achieve this (preferably through some attribute/annotation or something)?
Can’t you simply use the Control.Parent property? Granted, it will not be set in the constructor of your control, but the typical way to overcome that is by implementing ISupportInitialize and doing the work in the EndInit method.
Why do you need the reference back to the owing control?
Here, if you create a new console application, and paste in this content to replace the contents of Program.cs, and run it, you’ll notice that in .EndInit, the Parent property is correctly set.