using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.Design; using System.Windows.Forms; namespace Samples { public partial class Controller : Component { public Controller() { InitializeComponent(); } public Controller(IContainer container) { container.Add(this); InitializeComponent(); } [Browsable(false)] public virtual Form OwnerForm { get { return _ownerForm; } set { _ownerForm = value; } } Form _ownerForm = null; public override ISite Site { get { return base.Site; } set { base.Site = value; this.OwnerForm = FindForm(); } } Form FindForm() { IReferenceService referenceService = (IReferenceService)GetService(typeof(IReferenceService)); if (referenceService != null) { // 取得组件所在的窗体对象 object[] parent = referenceService.GetReferences(typeof(Form)); Form form = parent[0] as Form; return form; } return null; } } }