集合编辑器,留着研究下。

来源:互联网 发布:淘宝网购十字绣 编辑:程序博客网 时间:2024/06/14 01:03
C#.net]自定义控件中CollectionBase的使用 集合编辑器
2012-04-19 14:29
[原][C#.net]自定义控件中CollectionBase的使用 集合编辑器
2009-11-02 17:50

最近研究自定义的控件,CollectionBase 真的是很好的东西,效果图如下

其中包括很多经典代码:怎么给List命名?怎么写name的重复报警等?怎么通过index或name找list等

部分代码网上找到,部分代码自己编写,总之还算完善,愿结识喜欢做控件的朋友共同探讨之~~

    public class FieldItems : CollectionBase
    {

        public FieldItems(PxAutoGenTxtPanel _pxAutoGenTxtPanel)
            : base()
        {

            //

            // TODO: 在此处添加构造函数逻辑
            pxAutoGenTxtPanel = _pxAutoGenTxtPanel;
            //

        }
        private PxAutoGenTxtPanel pxAutoGenTxtPanel = null;
        PublicClass pubClass = new PublicClass();

        public FieldItem this[int index]
        {

            get
            {
                return (FieldItem)base.List[index];
            }

            set
            {
                base.List[index] = value;
                //this[index].FieldItems = this;
                //this[index].PxAutoGenTxtPanel = pxAutoGenTxtPanel;
                //this[index].Index = index;
            }

        }

        public FieldItem this[string name]
        {
            get
            {
                foreach (FieldItem fI in this)
                {
                    if (fI.Name == name)
                    {
                        return fI;
                    }
                }
                return null;
            }
        }


        public void Add(FieldItem aItem)
        {
            base.List.Add(aItem);
        }

        //public int IndexOf(QueryItem aItem)
        //{
        //    return List.IndexOf(aItem);
        //}

        public void Remove(int index)
        {

            if (index < base.Count - 1 && index > 0)
            {
                base.List.RemoveAt(index);
            }

        }

        protected override void OnInsertComplete(int index, object value)
        {
            base.OnInsertComplete(index, value);
            pubClass.AutoGenTxt(pxAutoGenTxtPanel, this);
        }

        protected override void OnRemoveComplete(int index, object value)
        {
            base.OnRemoveComplete(index, value);
            pubClass.AutoGenTxt(pxAutoGenTxtPanel, this);
        }

        protected override void OnSet(int index, object oldValue, object newValue)
        {
            base.OnSet(index, oldValue, newValue);
            pubClass.AutoGenTxt(pxAutoGenTxtPanel, this);
        }

    }


    public class FieldItem
    {
        public FieldItem()
        {
            this.Name = "item" + COUNT.ToString();
            COUNT++;
        }

        private static int COUNT = 0;//个数
        private string _name; //名称。

        PublicClass pubClass = new PublicClass();
        private string _lblText = "label";
        private FieldItems _fieldItems = null;
        private PxAutoGenTxtPanel _pxAutoGenTxtPanel = null;
        private int _index = 0;

        private static string[] strName = new string[1000] ;

        [Category("Label属性"), Description("关键字段名")]
        public string LblText
        {
            get
            {
                return _lblText;
            }
            set
            {
                this._lblText = value;
                if (_pxAutoGenTxtPanel != null && _fieldItems != null)
                {
                    pubClass.AutoGenTxt(_pxAutoGenTxtPanel, _fieldItems);
                }

            }
        }


        [Category("PX属性"), Description("Item名称")]
        public string Name
        {
            get
            {
                strName[COUNT] = _name;
                return _name;

            }
            set
            {
                bool blIsSucc = true;
                for (int i = 0; i < strName.Length; i++)
                {
                    if (strName[i] == value)
                    {
                        blIsSucc = false;
                        throw new ArgumentException("集合中的元素不能重名。请修改名称。"+ _name);
                    }
                }
                if (blIsSucc == true)
                {
                    _name = value;
                }
            }
        }

        #region TextBox属性
        private string _txtBoxText = "";
        [Category("textBox属性"), Description("TextBox的值")]
        public string TxtBoxText
        {
            get
            {
                return _txtBoxText;
            }
            set
            {
                _txtBoxText = value;
            }
        }

        private bool _txtBoxReadonly = true;

        [Category("textBox属性"), Description("TextBox的Readonly属性")]
        public bool TxtBoxReadonly
        {
            get
            {
                return _txtBoxReadonly;
            }
            set
            {
                _txtBoxReadonly = value;
            }
        }
        #endregion

        #region 接收父值
        public FieldItems FieldItems
        {
            set
            {
                _fieldItems = value;
            }
        }

        public PxAutoGenTxtPanel PxAutoGenTxtPanel
        {
            set
            {
                _pxAutoGenTxtPanel = value;
            }
        }


        public int Index
        {
            set
            {
                _index = value;
            }
        }
#endregion

    }

原创粉丝点击