首页 > 其他 > 详细

如何在组件(Component中)模拟用户控件(UserControl)中FindForm()?

时间:2015-10-08 18:21:29      阅读:175      评论:0      收藏:0      [点我收藏+]
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;
        }
    }
}

如何在组件(Component中)模拟用户控件(UserControl)中FindForm()?

原文:http://www.cnblogs.com/eastson/p/4861641.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!