title: Does a Winforms Control raise an event when it is added to a form
body: <p>I'm working on some custom <code>Control</code> classes and need to do some initialization on them which is dependent on them being added to a form.  Is there an event that fires when this happens?</p>
  
  <p>I think this sample should be sufficient to show what I'm trying to do:</p>
  
  <pre><code>public interface IMyForm
  {
      ISomeObject SomeObject {get; set; }
  }
  
  class MyForm : IMyForm
  {
      //eg InitializeComponent() as well as several others called at later points
      private MethodThatAddsAControl()  
      {
          MyControl newControl = new MyControl();
          //other initialization as needed
  
          //does this raise an event in MyControl I can use to call
          //InitializationAfterBeingAddedToForm()?
          this.Controls.Add(newControl);   
      }
  }
  
  
  class MyControl : Control
  {
      InitializationAfterBeingAddedToForm()
      {
          //can't be done in the constructor because at that point FindForm() will return null
          (FindForm() as IMyForm).SomeObject.AnEvent += new EventHandler(SomeObject_AnEvent);
      }
  }
  </code></pre>
  
  <p>This is turning out to be more difficult than I initially realized, and I think I'm going to have to combine the suggestions from Bolu and Mike Dour.  The problem is that while some <code>MyControl</code>s are added directly to the form in which case Bolu's solution works perfectly.  Others are added to panels instead of being added directly to the form.  I think I've cobbled together a solution involving Bolu's solution for the former case with a few modifications to handle the case where the event is being raised by the panel being added, not the <code>MyControl</code> within it, and Mikes to handle the case of <code>MyControl</code>s being added to the panel after the constructor has finished running.  I'll have to test this more tomorrow morning before I feel confident that it worked.</p>
  
  <p>Error message requested by Bolu when I try using his suggestion in the designer:</p>
  
  <pre class="lang-none prettyprint-override"><code>Failed to create component 'MyControl'.  The error message follows:
   'System.MissingMethodException: Constructor on type 'MyNamespace.MyControl' not found.
     at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
     at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
     at System.ComponentModel.Design.DesignSurface.CreateInstance(Type type)
     at Microsoft.VisualStudio.Design.VSDesignSurface.CreateInstance(Type type)
     at System.ComponentModel.Design.DesignerHost.System.ComponentModel.Design.IDesignerHost.CreateComponent(Type componentType, String name)
     at System.ComponentModel.Design.DesignerHost.System.ComponentModel.Design.IDesignerHost.CreateComponent(Type componentType)
     at System.Drawing.Design.ToolboxItem.CreateComponentsCore(IDesignerHost host)
     at System.Drawing.Design.ToolboxItem.CreateComponentsCore(IDesignerHost host, IDictionary defaultValues)
  ...'
  </code></pre>
  
  <p>And the constructors in place when I got the error.</p>
  
  <pre><code>public MyControl(Form parent)
  {
      _parent = parent as IMyForm;
      parent.ControlAdded += new ControlEventHandler(parent_ControlAdded);
      Initialize();  //does rest of initialization
  }
  
  public TimelineControl(Form parent, Panel container)
  {
      _parent = parent as IMyForm;
      container.ControlAdded += new ControlEventHandler(parent_ControlAdded);
      Initialize();  //does rest of initialization
  }
  </code></pre>