从自定义控件设计器到自定义控件的调用方法

时间:2019-07-18 10:14:50

标签: c# winforms custom-controls

我已经创建了一个从 Panel 继承的 Custom TabControl ,并且我已经创建了一个从System.Windows继承的 custom Designer 。 Forms.Design。 ControlDesigner 。 在设计器内部,我想在自定义TabControl中调用方法,以便从DesignerVerb在我的自定义TabControl中添加新标签。

我试图调用将该控件转换为CustomTabControl的函数,它执行Method,但返回一个警告消息框“ Circular Control Reference。控件不能是父项或自身的专有权。”


    #region CustomTabControlDesigner Class

    internal class CustomTabControlDesigner:System.Windows.Forms.Design.ControlDesigner
        {

        #region FIELDS

        private DesignerVerbCollection _actions;

        #endregion FIELDS

        #region OVERS

        public override DesignerVerbCollection Verbs
        {
            get
            {
                if (_actions == null)
                {
                    _actions = new DesignerVerbCollection();
                    _actions.Add(new DesignerVerb("Add Tab", new EventHandler(AddNewPage)));
                }
                return _actions;
            }
        }

        #endregion OVERS

        #region HANDLERS

        public void AddNewPage(object sender, EventArgs e)
        {
            var control = Control as CustomTabControl;
            control?.AddNewPage();
        }

        #endregion HANDLERS
#endregion CustomTabControlDesignerClass

#region CustomTabControl Class
    [Designer(typeof(CustomTabControlDesigner))]
    public class CustomTabControl : Panel, ICustomTabControl
    {
        public void AddNewPage()
        {
            CustomContext ct = new CustomContext(AutoIncrementId.ToString());
            ct.Header.Click += OnHeaderClick;
            IteratorListContexts.Add(ct);
            IteratorListContexts.CurrentItem.Page.Visible = false;
            IteratorListContexts.SetCurrentItem(ct);

            AddHeader(ct.Header);
            CheckToShowNavigationButtonPanel();
            base.Controls.Add(IteratorListContexts.CurrentItem.Page);
            IteratorListContexts.CurrentItem.Page.Visible = true;
            IteratorListContexts.CurrentItem.Page.BringToFront();
        }
    }
#endregion CustomTabControl Class

0 个答案:

没有答案