473,401 Members | 2,139 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,401 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 1284
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.