473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Custom code generation like MainMenu?

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

Jul 21 '05 #1
3 1283
Actualy I should clarify what I would really like is something like

myComponent.Owner=this

Inside a form. Setting my Property with the form.

Jul 21 '05 #2
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
<ts******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Actualy I should clarify what I would really like is something like

myComponent.Owner=this

Inside a form. Setting my Property with the form.

Jul 21 '05 #3
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
<ts******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Actualy I should clarify what I would really like is something like

myComponent.Owner=this

Inside a form. Setting my Property with the form.


Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Google Mike | last post by:
Tell me if this can be done, and if I have a misconception here. I am writing an app that will be served up in an app farm, and therefore I need to move the session information to the client, not...
7
by: Steve Jorgensen | last post by:
Here is some code to generate code for raising and getting information about custom errors in an application. Executing the GenerateXyzErrDefs procedure generates the code. <tblXyzError>...
7
by: Martin Schulze | last post by:
Hello, i tried to compose myself a custom usercontrol which is derieved from System.Windows.Forms.UserControl. It contains 2 comboboxes and one textbox (which are also custom controls, but...
3
by: Carlos Guzmán Álvarez | last post by:
Hello: There are any way to make a MainMenu that creates the menu items based a custom MenuItem implementation ?? Thanks in advance !! --
3
by: Michael | last post by:
Hi all.. I have a solution containing 5 projects, one of which is a custom control. In one of the projects, I dragged the .ascx file from the custom control project and dropped it onto a form....
3
by: tsteinke | last post by:
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; ...
6
by: cj | last post by:
Lets just take this example I'm looking at now. I'm looking at the help screen titled .NET Framework Class Library FolderBrowserDialog Class . It gives an example at the bottom that begins with:...
2
by: trialproduct2004 | last post by:
Hi all, I am having application in whihc i am inserting menuitem dynamically. When i add menuitem to mainmenu, i want to pass extra parameter to eventargs. So what i did is derived class from...
2
by: Sunfire | last post by:
I put some code in the master page load event and find that for some reason, all of the stuff on the page got moved down 2 lines. Is there any way to fix this?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.