PC Review


Reply
Thread Tools Rate Thread

Custom code generation like MainMenu?

 
 
tsteinke@gmail.com
Guest
Posts: n/a
 
      16th Feb 2005
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

 
Reply With Quote
 
 
 
 
tsteinke@gmail.com
Guest
Posts: n/a
 
      16th Feb 2005
Actualy I should clarify what I would really like is something like

myComponent.Owner=this

Inside a form. Setting my Property with the form.

 
Reply With Quote
 
Mick Doherty
Guest
Posts: n/a
 
      16th Feb 2005
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


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Actualy I should clarify what I would really like is something like
>
> myComponent.Owner=this
>
> Inside a form. Setting my Property with the form.
>



 
Reply With Quote
 
tsteinke@gmail.com
Guest
Posts: n/a
 
      16th Feb 2005
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 Doherty wrote:
> 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
>
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Actualy I should clarify what I would really like is something like
> >
> > myComponent.Owner=this
> >
> > Inside a form. Setting my Property with the form.
> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom code generation like Timer.SynchronizingObject? tsteinke@gmail.com Microsoft Dot NET 0 21st Feb 2005 07:35 PM
Custom Attributes and Code Generation S-t-a-r-C-i-t-e Microsoft Dot NET Framework 1 1st Jun 2004 03:00 AM
Custom Attributes and Code Generation S-t-a-r-C-i-t-e Microsoft Dot NET 0 29th May 2004 04:56 AM
Custom ToolBar & Code generation =?Utf-8?B?VGltbQ==?= Microsoft Dot NET Framework 0 21st May 2004 10:06 AM
Custom MainMenu =?Utf-8?B?TmlsZXNo?= Microsoft Dot NET Framework Forms 0 18th Feb 2004 02:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:51 AM.