using System; using System.ComponentModel; using System.ComponentModel.Design; using System.Reflection; using System.Text; using System.Windows.Forms; namespace AlarmComponentSample { public class HostComponentDesigner : ComponentDesigner { public override void InitializeNewComponent(System.Collections.IDictionary defaultValues) { base.InitializeNewComponent(defaultValues); IComponent customComponent = this.Component; IComponent parentComponent = this.ParentComponent; // Don't bother if parent is not a container if( !(parentComponent is ContainerControl) ) return; // If component has HostPropertyAttribute, // get the property it specifies and set it with the parent component string propertyName = "Host"; AttributeCollection attributes = TypeDescriptor.GetAttributes(customComponent); foreach( Attribute attribute in attributes ) { if( attribute is HostPropertyAttribute ) { HostPropertyAttribute hpAttribute = (HostPropertyAttribute)attribute; if( !string.IsNullOrEmpty(hpAttribute.PropertyName) ) propertyName = hpAttribute.PropertyName; break; } } // Set property with host container PropertyInfo property = customComponent.GetType().GetProperty(propertyName); if( property != null ) { property.SetValue(customComponent, (ContainerControl)parentComponent, null); } } } }