检查是否已将自定义控件添加到表单中

时间:2011-09-23 01:51:08

标签: c# winforms user-controls

我正在创建一个自定义C#控件(表单的标题栏)。一个表单只能有一个标题栏,这就是为什么我想知道的东西:当用户(程序员)将我的标题栏添加到他的表单时,是否有任何方法我可以检查ParentForm是否已经包含我的标题栏,如果可以的话我取消添加我的控件的另一个实例?

我知道如何执行检查以查看ParentForm包含的控件类型,但是当我的控件从工具箱中删除到表单时会引发什么事件,以及如何在必要时“取消”我的控件的布局?

3 个答案:

答案 0 :(得分:2)

你应该read in-depth关于.NET中可用的designer technologies,因为这些不是我称之为生产就绪的例子。但是,这为您提供了一个可靠的开始,两个代码片段都能满足您的要求。

对于设计时,您可以覆盖控件中的设计器站点并执行以下操作:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Diagnostics;

namespace WindowsFormsControlLibrary1 {
    public partial class DebugControl : UserControl {
        public DebugControl() {
            InitializeComponent();
        }

        public override ISite Site
        {
            get
            {
                return base.Site;
            }
            set
            {
                base.Site = value;
                IComponentChangeService service = (IComponentChangeService)GetService(typeof(IComponentChangeService));
                service.ComponentAdding += (sender, e) => {
                    IDesignerHost host = (IDesignerHost)value.Container;
                    Component component = (Component)host.RootComponent;
                    if (component as Form != null)
                    {
                        Form form = (Form)component;
                        foreach (Control c in form.Controls)
                        {
                            if (c.GetType() == this.GetType())
                            {
                                throw new System.ArgumentException("You cannot add two of these controls to a form!");
                            }
                        }
                    }
                };
            }
        }
    }
}

对于表单中的运行时,您可以覆盖OnControlAdded并执行以下操作:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using WindowsFormsControlLibrary1;

namespace WindowsFormsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            Controls.Add(new DebugControl());
        }

        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);
            if (e.Control.GetType() == typeof(DebugControl))
            {
                int count = 0;
                foreach (Control c in Controls)
                {
                    if (c is DebugControl)
                    {
                        count++;
                    }
                }
                if (count > 1)
                {
                    Controls.Remove(e.Control);
                    Debug.Assert(false, "Cannot add two of these controls!");
                }
            }
        }
    }
}

有多种方法可以做到这一点,但这些是粗略的示例,请注意。阅读.NET框架的设计时支持,它非常​​丰富,并且有大量的文档。另一种方法是实现自定义设计器并实现CanBeParentedTo和CanParent,但是请注意,当您的控件从ToolBox到您的表单时,不会调用CanBeParentedTo。

答案 1 :(得分:0)

您可以使用有效Controls的{​​{1}}个集合。

答案 2 :(得分:0)

处理ParentChanged事件,检查父项Controls,并在必要时抛出异常。

相关问题