I have a non-visual class like (avalible in the toolbox) public class MyClass: Component { public MyClass() {...} }
In this class I have some asyncronus external events that needs to be syncronized with the "main - gui - thread" that consumes events from "MyClass"...
Before I made a "toolbox" component of "MyClass" I hade a constructor like public class MyClass: Component { public MyClass(Control parent) {...} } This way I had the "parent" object to sync with... But using MyClass as a component in the "toolbox" forces me to have a constructor without any parameters...
I need to get hold of the parents "Control - object" inside of "MyClass". How can this be done?
Tanks for any help! |
| Zapp Tuesday, September 26, 2006 12:40 PM |
I have now solved my problem (but kind of ugly...)
This way I get the parent object at runtime - all hidden for the user! (This object runns in the "GUI main thread" and can be used to syncronize generated events!)
This nice article answers a lot of questions (I you have time to really read it...) http://msdn.microsoft.com/msdnmag/issues/03/04/Design-TimeControls/
Tanks all for your help!
public class MyClass: Component { private Control parent = null;
// Constructor public MyClass(IContainer container) { if((container != null) && (container.Components.Count > 0)) { parent = (Control)container.Components[0]; } }
// GUI object used to syncronize the mu2guiclient socket events with the main GUI thread [BrowsableAttribute(false)] public Control Parent { get { return parent; } set { parent = value; } } ... ... }
|
| Zapp Wednesday, September 27, 2006 10:14 AM |
Seems like the FindForm() does the job as well! Don't know why I missed this before? When just getting hold of the form anything can be digged out and used...
|
| Zapp Monday, April 16, 2007 6:54 AM |
Hi,
You can have both constructors. When you create a new component in Visual Studio, the following code is generated:
public partial class Component1 : Component { public Component1() { InitializeComponent(); }
public Component1(IContainer container) { container.Add(this); InitializeComponent(); } }
Andrej |
| Andrej Tozon Tuesday, September 26, 2006 12:59 PM |
have you tried GetContainerControl?
|
| joeycalisay Tuesday, September 26, 2006 1:29 PM |
Ok a bit on the way...
I can have a constructor like this... Buthow toget the parent Control object (inside the constructor)?
public MyClass(IContainer container) { Control parent = (Control)container // <--raises anexeption! }
|
| Zapp Tuesday, September 26, 2006 1:56 PM |
Hi all!
After spending some more time trying to solve my problem I found this old thread; http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=32003&SiteID=1
"I've created a class that inherits from ComponentModel.Component. I can host it on a form, and it works fine. Problem is that I need to retrieve a reference to the parent form, early in the process. If I create a public property in the component, I can set it from the Form's Load event, but that seems redundant. The darned thing is HOSTED on the form, for gosh sake. How do I retrieve a reference to the parent form from within the component? I've tried using the Site and Container properties of the component, but those of course aren't what I need. I'm sure this must be simple, but I'm not seeing it. Any help appreciated."
This is exactly my problem! But no one seems to know the answer - so maybe it's not possible?
Tanks for any help with this matter!
|
| Zapp Wednesday, September 27, 2006 7:17 AM |
I have now solved my problem (but kind of ugly...)
This way I get the parent object at runtime - all hidden for the user! (This object runns in the "GUI main thread" and can be used to syncronize generated events!)
This nice article answers a lot of questions (I you have time to really read it...) http://msdn.microsoft.com/msdnmag/issues/03/04/Design-TimeControls/
Tanks all for your help!
public class MyClass: Component { private Control parent = null;
// Constructor public MyClass(IContainer container) { if((container != null) && (container.Components.Count > 0)) { parent = (Control)container.Components[0]; } }
// GUI object used to syncronize the mu2guiclient socket events with the main GUI thread [BrowsableAttribute(false)] public Control Parent { get { return parent; } set { parent = value; } } ... ... }
|
| Zapp Wednesday, September 27, 2006 10:14 AM |
Zapp:
I ran into something similar, but may be on a different path to your problem since you are using component as a base class. With mysolution below for user controls, I am wondering if you are allowed to modify the default contructor in the designer with one that has an additional argument to pass "this" for the parent's ContainerControl. The declarations for the child controls are outside of the designer extract below.
[Designer extract]
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion
[/Designer extract]
[Solution for User Controls]
namespace System.Windows.Forms
{
public class ContainerControl : ScrollableControl, IContainerControl
{
...
public Form ParentForm { get; }
...
}
public class UserControl : ContainerControl
{...}
}
namespace MyProject.UserControls
{
public partial class MyControl : UserControl
{
public MyControl ()
{
InitializeComponent();
Parent = (MyControl)this.ParentForm();
}
private MyParentForm _parent = null;
[ BrowsableAttribute(false)]
public MyParentForm Parent
{
get { return _parent; }
set { _parent = value; }
}
} // public partial class MyControl
} // end namespace MyProject.UserControls
[/Solution for User Controls]
//eof |
| jmsigler2 Monday, October 23, 2006 8:51 PM |
Seems like the FindForm() does the job as well! Don't know why I missed this before? When just getting hold of the form anything can be digged out and used...
|
| Zapp Monday, April 16, 2007 6:54 AM |