PropertyGrid中实现自定义参数设置,以bit数组设置byte值功能

    类型属性栏中以byte类型显示和修改,但是又可以通过点击类型的详细信息设置每一个bit的值,最后设置到byte上。

    如下图:byteEditor是byte类型,但是设置时可以通过协议编辑器以bit进行设置。每一bit对应一个bool类型的数据,此处是枚举。

    byteEditor对应的类型继承了实现形式System.ComponentModel.Design.ArrayEditor类。其他类型的XXXEditor也是类似的。

    实现方式是通过完全实现XXXEditor的virtual方法,通过查看每一个方法的执行顺序,修改其内部实现。

本文代码未进行优化。

设置前:

设置时:成员 名称也是可以设置的

代码:

byte设置

        [DisplayName("byteEditor")]
        [Browsable(true)]
        [Description("byteEditor1")]
        [Category("扩展")]
        [Editor(typeof(ByteEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public byte ByteEditor
        {
            get;
            set;
        }

 bit设置:

public class ByteEditor : System.ComponentModel.Design.ArrayEditor
    {
        BitValue[] byteArray = new BitValue[8];

        public ByteEditor(Type type)
            : base(type)
        {

        }

        protected override object SetItems(object editValue, object[] value)
        {
            editValue = BitConverter.GetBytes((byte)editValue);
            return base.SetItems(editValue, value);
        }

        protected override object[] GetItems(object editValue)
        {

            for (int i = 0; i < 8; i++)
            {
                byteArray[i] = new BitValue()
                {
                    jishufangshi = (jishufangshiEnum)
                    Enum.Parse(
                    typeof(jishufangshiEnum),
                    ((byte)(0x01 & ((int)(byte)editValue) >> i)).ToString()
                    )
                };
            }
            return base.GetItems(byteArray);
        }

        protected override Type CreateCollectionItemType()
        {
            return typeof(BitValue);
        }

        protected override string GetDisplayText(object value)
        {
            return base.GetDisplayText(value);
        }

        protected override System.Collections.IList GetObjectsFromInstance(object instance)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        protected override bool CanRemoveInstance(object value)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        protected override object CreateInstance(Type itemType)
        {
            return CreateInstance(itemType);
            //throw new Exception("The method or operation is not implemented.");
        }

        //public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        //{
        //    return base.GetEditStyle(context);
        //}

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return base.GetEditStyle(context);
            //throw new Exception("The method or operation is not implemented.");
        }

        protected override void ShowHelp()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        protected override void CancelChanges()
        {
            base.CancelChanges();
            //throw new Exception("The method or operation is not implemented.");
        }

        protected override Type[] CreateNewItemTypes()
        {
            return base.CreateNewItemTypes();
            //throw new Exception("The method or operation is not implemented.");
        }

        protected override void DestroyInstance(object instance)
        {
            base.DestroyInstance(instance);
            //throw new Exception("The method or operation is not implemented.");
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {

            return base.EditValue(context, provider, value);

            //throw new Exception("The method or operation is not implemented.");
        }

        //public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        //{
        //    return base.EditValue(context, provider, value);
        //}

        protected override bool CanSelectMultipleInstances()
        {
            return base.CanSelectMultipleInstances();
            //throw new Exception("The method or operation is not implemented.");
        }

        protected override CollectionForm CreateCollectionForm()
        {
            CollectionForm collectionForm = base.CreateCollectionForm();
            collectionForm.Text = "协议编辑器";
            collectionForm.Size = new System.Drawing.Size(586, 400);
            foreach (System.Windows.Forms.Control table in collectionForm.Controls)
            {
                if (!(table is System.Windows.Forms.TableLayoutPanel)) { continue; }
                foreach (System.Windows.Forms.Control c1 in table.Controls)
                {
                    //如果属性值发生改变,则通知编辑器已改变
                    if (c1 is PropertyGrid)
                    {
                        PropertyGrid propertyGrid = (PropertyGrid)c1;
                        propertyGrid.HelpVisible = true;
                        propertyGrid.ToolbarVisible = false;
                        //propertyGrid.PropertyValueChanged += new PropertyValueChangedEventHandler(GotModifiedHandler);
                        //propertyGrid.SelectedObjectsChanged += new EventHandler(propertyGrid_SelectedObjectsChanged);
                    }
                    else if (c1 is TableLayoutPanel)
                    {
                        foreach (System.Windows.Forms.Control c2 in c1.Controls)
                        {
                            if (!(c2 is Button)) { continue; }
                            Button button = (Button)c2;
                            //如果增加或删除Collection中的Item了,通知编辑器已改变
                            if (button.Name == "addButton" || button.Name == "removeButton")
                            {
                                //隐藏增加和删除按钮
                                button.Visible = false;
                            }

                            if (button.Name.Equals("okbutton", StringComparison.OrdinalIgnoreCase))
                            {
                                button.Click += new EventHandler(button_Click);
                            }
                        }
                    }
                    else if (c1 is Button)
                    {
                        Button btn = c1 as Button;
                        if (btn != null && (btn.Name == "downButton" || btn.Name == "upButton"))
                        {
                            btn.Visible = false;
                        }
                    }
                }
            }
            return collectionForm;
        }

        void button_Click(object sender, EventArgs e)
        {
            var btn = sender as Button;
            var form = btn.Parent.Parent.Parent as CollectionForm;

            if (form != null)
            {

                if (byteArray[0] != null)
                {
                    int val = (int)this.byteArray[0].jishufangshi +
                    ((int)byteArray[1].jishufangshi << 1) +
                    ((int)byteArray[2].jishufangshi << 1);
                    //(context.Instance as T1).ByteEditor = (byte)val;
                    form.EditValue = (byte)val;

                }
            }
        }
    }

    public enum jishufangshiEnum
    {
        jishu = 0,
        bujisu = 1,
    }

    public class BitValue
    {
        [Category("jiben")]
        [DisplayName("计数方式")]
        [Description("nan")]
        [Browsable(true)]
        public jishufangshiEnum jishufangshi
        {
            get;
            set;
        }

        public override string ToString()
        {
            return "jishufangshi" + (((int)jishufangshi).ToString());
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值