Custom code generation like MainMenu?

  • Thread starter Thread starter tsteinke
  • Start date Start date
T

tsteinke

I am trying to make a Component that is aware of the Form that it is
added to. I noticed that when you drop a MainMenu onto a form the
designer generates the code.

this.Menu = this.mainMenu1;

Obviously the component knows that it is being added to a form and
knows what property it needs to set on it.

How would one go about implementing this? Is this an example of Custom
code generation? A custom designer? or is there a far more straight
forward way of doing this?

Thank you in advance.

Tom
 
Actualy I should clarify what I would really like is something like

myComponent.Owner=this

Inside a form. Setting my Property with the form.
 
I don't know if there's a better way, but I do it like this:

\\\
using System.ComponentModel;
using System.Windows.Forms;
using System.ComponentModel.Design;

public class MyComponent : Component

private Form owner;

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public Form Owner
{
get{return owner;}
set{owner = value;}
}

public override ISite Site
{
get{return base.Site;}
set
{
base.Site = value;
IDesignerHost host = (IDesignerHost)
this.GetService(typeof(IDesignerHost));
if (host != null)
if (host.RootComponent is Form)
owner = (Form)host.RootComponent;
}
}

}
///
 
During Runtime "host" ends up being null causing a crash.

I got this to work by manually putting in the code with a
DesignerSerializer but this seems like overkill and potentially
problems.

I realize that Timer does this with it's SynchronizingObject, I wish I
could see how it does it.

Maybe an IExtenderProvider? Possibly

Mick said:
I don't know if there's a better way, but I do it like this:

\\\
using System.ComponentModel;
using System.Windows.Forms;
using System.ComponentModel.Design;

public class MyComponent : Component

private Form owner;

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public Form Owner
{
get{return owner;}
set{owner = value;}
}

public override ISite Site
{
get{return base.Site;}
set
{
base.Site = value;
IDesignerHost host = (IDesignerHost)
this.GetService(typeof(IDesignerHost));
if (host != null)
if (host.RootComponent is Form)
owner = (Form)host.RootComponent;
}
}

}
///

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


Actualy I should clarify what I would really like is something like

myComponent.Owner=this

Inside a form. Setting my Property with the form.
 
Back
Top