Why custom control task pane does not update its properties?

北城余情 提交于 2020-01-13 14:57:11

问题


I have designed a custom panel which can expand or collapse form at run time.

When I change its height from custom designed task, it does not update it.

Code of my control class:

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;

[Designer(typeof(MyControlDesigner))]
 public partial class ExpandCollapsePanel : UserControl
    {    
        private bool flag = false;
        private Size size;
        public int usrVerticalSize;

        public ExpandCollapsePanel()
        {
            InitializeComponent();

        }       
        [DefaultValueAttribute(true)]
        public int SetVerticalSize
        {
            get
            {
                return usrVerticalSize;
            }
            set
            {
                usrVerticalSize = value;
            }
        }

Code of taskpanedesign class:

namespace ExpandCollapseFormLibrary
{
    class CustomDialogue : ControlDesigner
    {
        private DesignerActionListCollection actionLists;
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (actionLists == null)
                {
                    actionLists = new DesignerActionListCollection();
                    actionLists.Add(new MyActionListItem(this));
                }
                return actionLists;
            }
        }
    }
    internal class MyActionListItem : DesignerActionList
    {
        public MyActionListItem(ControlDesigner owner) : base(owner.Component)
        {
        }
        public override DesignerActionItemCollection GetSortedActionItems()
        {
            var items = new DesignerActionItemCollection();
            //items.Add(new DesignerActionTextItem("Hello world", "Misc"));
            items.Add(new DesignerActionPropertyItem("Checked", "Vertical Drop Down Size"));
            return items;
        }

        public int Checked
        {
            get { return ((ExpandCollapsePanel)base.Component).SetVerticalSize; }
            set { ((ExpandCollapsePanel)base.Component).SetVerticalSize = value; }
        }

    }    
}

When I change the value the Form1(where drag and dropped) designed class keep it permanently.


回答1:


the SetVerticalSize property value of your custom pane's is really changed, but the problem is that the designer host does not know about it at all. To notify the designer host about your custom pane changing you should implement something like this (I suggest you read the IComponentChangeService MSDN article for more details):

    int usrVerticalSize;
    [DefaultValue(true)]
    public int SetVerticalSize {
        get { return usrVerticalSize; }
        set {
            FireChanging(); //changing notification 
            try {
                usrVerticalSize = value;
            }
            finally { FireChanged(); } //changed notification 
        }
    }
    void FireChanging() {
        IComponentChangeService service = GetComponentChangeService();
        if(service != null)
            service.OnComponentChanging(this, null);
    }
    void FireChanged() {
        IComponentChangeService service = GetComponentChangeService();
        if(service != null)
            service.OnComponentChanged(this, null, null, null);
    }
    IComponentChangeService GetComponentChangeService() {
        return GetService(typeof(IComponentChangeService)) as IComponentChangeService;
    }


来源:https://stackoverflow.com/questions/7818284/why-custom-control-task-pane-does-not-update-its-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!