Get the parent Control object...

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!




Answer this question

Get the parent Control object...

  • KitGreen

    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



  • Evan Hennis

    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!



  • GlennZarb

    have you tried GetContainerControl


  • Suresh .M.V

    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; }
      }
     
      ...
      ...
    }

     



  • litewoheat

    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...


  • Dejan Kocijasevic

    Ok a bit on the way...

    I can have a constructor like this...
    But how to get the parent Control object (inside the constructor)

    public MyClass(IContainer container) {
    Control parent = (Control)container // <-- raises an exeption!

    }



  • ttad

    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 my solution 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


  • Get the parent Control object...