Dev Center > Windows Forms Forums > Windows Forms Designer > Child is not a Child Control of this Parent

Proposed Answer Child is not a Child Control of this Parent

  • Friday, December 02, 2005 9:43 PM
     
     
    Hi, I have created a userControl which I want to be able to draw and add new components during design time. I have added the following attribute to my class.

    [Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design",
             typeof(System.ComponentModel.Design.IDesigner))]
        public class RollUpTab : System.Windows.Forms.UserControl

    Which has a GroupBox as part of it.

    Within this class I overide the OnControlAdded method as follows
    protected override void OnControlAdded(ControlEventArgs e)
      {
    this.gpbox_MainGroup.Controls.Add(e.Control);
    }
    Now when ever I add a control to my control at design time I get this error mesage "Child is not a Child Control of this Parent".

    I can not find any references as to what this is? Can anyone help or explain to me what I am doing wrong.

    Thanks

All Replies

  • Wednesday, December 14, 2005 5:11 PM
     
     
    Peter,

    Did you ever figure out what the problem was? I'm having the same issue right now and not sure what the cause is.

    Thanks,
    Rachael
  • Saturday, December 17, 2005 4:26 PM
     
     

    Myguess is that it has to do with the fact that the designer is setting the parent to the form and not your control and then you are trying to add it to your control when it already has a parent.

    One workaround would be to create a new instance of the control based on the type, add that instance to your usercontrol and dispose of the other one.

    Another way would be to get the Idesigner service for the Form and remove the control and set a new instance in your designer.

    Keep in mind you must use the idesigner createcomponent so that it gets sited, either way you go above.

    Happy Holidays

    Joe

  • Tuesday, January 03, 2006 12:41 PM
     
     

    I am getting the same error message while removing a control.

    In my case, i have a Tab Page and a form over it. I have a check - on the event TabPage_ControlAdded that if any control other than form is added to the Tab Page then remove the control from Tab Page. The component (control) added gets removed but throws the error message - Child' is not a child control.....

    Please suggest.

  • Thursday, January 05, 2006 7:05 PM
     
     

    You need set the Parent property for the component to the rootdesigner: This sample add components into a designer host. All components are into a SortedList object. Note that a PropertyDescriptorCollection is requerid.

    IComponent m_customcomponent;
    PropertyDescriptorCollection props;
    PropertyDescriptor propParent;

    foreach (DictionaryEntry m_icomponent in m_qdictionary)

    {

    string m_assemblyname = m_icomponent.Value.GetType().Name;

    switch (m_assemblyname)

    {

    case "QLabel":

    QLabel m_qlabelcomponent = (QLabel)m_icomponent.Value;

    m_customcomponent = host.CreateComponent(typeof(QLabel));

    //Find the properties for the label control

    props = TypeDescriptor.GetProperties(m_qlabelcomponent);

    //We assign the properties from serialized object to the new component.

    ((QLabel)m_customcomponent).Texto = (string)props["Texto"].GetValue(m_qlabelcomponent);

    ((QLabel)m_customcomponent).Nombre = (string)props["Nombre"].GetValue(m_qlabelcomponent);

    // other properties

    //Get the Parent property

    propParent = props["Parent"];

    //Set the Parent property to the form:

    propParent.SetValue(m_customcomponent, host.RootComponent);

    break;

    case "QImage":

    break;

    //another members and code ommited...

    }

    }

     

    Regards

  • Friday, November 27, 2009 5:06 AM
     
     Proposed Answer
    Hello all,

    I started getting the same error in a Visual Studio 2008 project today. The following article and link resolved the issue: How can I drop controls within UserControl at Design time?: http://blogs.msdn.com/subhagpo/archive/2005/03/21/399782.aspx. Good Luck!
    • Proposed As Answer by Peter Smartt Wednesday, February 23, 2011 2:40 AM
    •  
  • Monday, November 30, 2009 8:17 PM
     
      Has Code
    hi, what i understand is you do not want to add any control to your UserControl, but add them to groupbox.

    To do so, you only create a designer to allow your groupbox accepts child controls at design time.
    Sample is below
    internal class MyOwnDesigner : ControlDesigner
        {
            private Control innerControl = null;        
            public override void Initialize(IComponent comp)
            {            
                base.Initialize(comp);
    
                MyControl cr = (MyControl )comp;          
                EnableDesignMode(cr.InnerControl, "InnerControl");
                innerControl = cr;
            }
        }   
    
    [Designer(typeof(MyOwnDesigner))]
        public partial class MyControl : UserControl
        {
    
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            public ControlInnerControl
            {
                get { return myGroupBox; }
            }
    
        }