C#.Net组件开发(高级篇) - 开发复杂的设计时组件编辑器
作者:C/S框架网  发布日期:2011/08/27 22:27:14
  C#.Net组件开发(高级篇) - 开发复杂的设计时组件编辑器

C#.Net组件开发(高级篇) - 开发复杂的设计时组件编辑器



积累了开发组件的基本知识,现在分享一个复杂的设计时组件编辑器,如下图:


贴图图片

该组件编辑器用于设计CSFrameworkComponent.InnerObject对象的内容。

贴图图片

InnerObject对象定义了两个字符类型的属性(ObjectCreator,ObjectName)和一个MyNodeCollection集合类型。

在窗体上拖放一个CSFrameworkComponent组件,点右键选择“编辑MyInnerObject对象”菜单。


贴图图片


InnerObject对象定义:



C# Code:

/// <summary>
/// 对象类型
/// </summary>
public class InnerObject
{
   public InnerObject()
   {
      _ObjectName = "对象名:" DateTime.Now.Ticks.ToString();
      _ObjectCreator = "Jonny Sun - C/S框架网";
      _MyNodeCollection = new MyNodeCollection();
   }
   
   private string _ObjectName = "";
   
   public string ObjectName
   {
      get { return _ObjectName; }
      set { _ObjectName = value; }
   }
   
   private string _ObjectCreator = "";
   
   public string ObjectCreator
   {
      get { return _ObjectCreator; }
      set { _ObjectCreator = value; }
   }
   
   private MyNodeCollection _MyNodeCollection;
   
   [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
   [Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]//属性编辑器
   public MyNodeCollection MyNodeCollection
   {
      get
      {
         return _MyNodeCollection;
      }
      set
      {
         _MyNodeCollection = value;
      }
   }
   
   public override string ToString()
   {
      //在属性编辑器内显示的名称
      return "(我的自定义对象)";
   }
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898



CSFrameworkComponent 组件:




  /// <summary>

    /// 用户自定义组件

    /// </summary>

    [ToolboxBitmap(typeof(CSFrameworkComponent), "CSFrameworkComponent.bmp")]

    [DesignTimeVisible(true)]

    [Designer(typeof(CSFrameworkComponentDesigner), typeof(IDesigner))]

    public partial class CSFrameworkComponent : Component

    {

        //对象集合

        private MyNodeCollection _MyNodeCollection;

 

        //嵌套的对象

        private InnerObject _MyInnerObject;

 

        //窗体设计器的接口引用,当拖拉组件到窗体上,值由构造器传入.

        private IContainer _Designer;

 

        public CSFrameworkComponent(IContainer container)

            : base()

        {

            if (container == null) throw new Exception("必须指定组件的容器!");

 

            container.Add(this);

 

            _Designer = container;//窗体的引用

 

            //实例化自定义组件

            _MyNodeCollection = new MyNodeCollection();

            _MyInnerObject = new InnerObject();

        }

 

        /// <summary>

        /// 移除窗体设计器内的代码

        /// </summary>

        /// <param name="disposing"></param>

        protected override void Dispose(bool disposing)

        {

            //设计环境下销毁对象(移除Designer.cs内的代码)

            if ((this.DesignMode) || (_Designer is IDesignerHost))

            {

                IDesignerHost host = (IDesignerHost)_Designer;

 

                //从窗体设计器删除当前组件嵌套的组件代码

                foreach (IComponent c in _MyNodeCollection)

                    host.Container.Remove(c); //销毁组件(删除Designer.cs内的代码)

 

                //从窗体设计器删除当前组件嵌套的组件代码

                foreach (IComponent c in _MyInnerObject.MyNodeCollection)

                    host.Container.Remove(c); //销毁组件(删除Designer.cs内的代码)

            }

 

            base.Dispose(disposing);

        }

 

        /// <summary>

        /// 用于测试打开Collection Editor的编辑器窗体

        /// </summary>

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

        [Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]//属性编辑器

        public MyNodeCollection MyNodeCollection

        {

            get

            {

                return _MyNodeCollection;

            }

            set

            {

                _MyNodeCollection = value;

            }

        }

 

        /// <summary>

        /// 用于测试打开自定义属性编辑器窗体

        /// </summary>

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

        [Editor(typeof(MyInnerObjectEditor), typeof(UITypeEditor))]//属性编辑器

        public InnerObject MyInnerObject

        {

            get

            {

                return _MyInnerObject;

            }

            set

            {

                _MyInnerObject = value;

            }

        }

    }





生成的代码Form1.Designer.cs:


C# Code:

partial class Form1
{
   /// <summary>
   /// Required designer variable.
   /// </summary>
   private System.ComponentModel.IContainer components = null;
   
   /// <summary>
   /// Clean up any resources being used.
   /// </summary>
   /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
   protected override void Dispose(bool disposing)
   {
      if (disposing && (components != null))
      {
         components.Dispose();
      }
      base.Dispose(disposing);
   }
   
   #region Windows Form Designer generated code
   
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {
      this.components = new System.ComponentModel.Container();
      this.button1 = new System.Windows.Forms.Button();
      this.csFrameworkComponent1 = new CSFramework.MyDesignEditor.CSFrameworkComponent(this.components);
      this.csFrameworkNode2 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode3 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode5 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode6 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode7 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode8 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode9 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode10 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode11 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode12 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode1 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode4 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode13 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode14 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode15 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.csFrameworkNode16 = new CSFramework.MyDesignEditor.CSFrameworkNode();
      this.SuspendLayout();
      //
      // button1
      //
      this.button1.Location = new System.Drawing.Point(50, 50);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(150, 30);
      this.button1.TabIndex = 0;
      this.button1.Text = "www.CSFramework.com";
      //
      // csFrameworkComponent1
      //
      this.csFrameworkComponent1.MyInnerObject.MyNodeCollection.Add(this.csFrameworkNode2);
      this.csFrameworkComponent1.MyInnerObject.MyNodeCollection.Add(this.csFrameworkNode3);
      this.csFrameworkComponent1.MyInnerObject.MyNodeCollection.Add(this.csFrameworkNode5);
      this.csFrameworkComponent1.MyInnerObject.MyNodeCollection.Add(this.csFrameworkNode6);
      this.csFrameworkComponent1.MyInnerObject.MyNodeCollection.Add(this.csFrameworkNode7);
      this.csFrameworkComponent1.MyInnerObject.MyNodeCollection.Add(this.csFrameworkNode8);
      this.csFrameworkComponent1.MyInnerObject.MyNodeCollection.Add(this.csFrameworkNode9);
      this.csFrameworkComponent1.MyInnerObject.MyNodeCollection.Add(this.csFrameworkNode10);
      this.csFrameworkComponent1.MyInnerObject.MyNodeCollection.Add(this.csFrameworkNode11);
      this.csFrameworkComponent1.MyInnerObject.MyNodeCollection.Add(this.csFrameworkNode12);
      this.csFrameworkComponent1.MyInnerObject.ObjectCreator = "Jonny Sun - C/S框架网555";
      this.csFrameworkComponent1.MyInnerObject.ObjectName = "对象名:634500426876739555555";
      this.csFrameworkComponent1.MyNodeCollection.Add(this.csFrameworkNode1);
      this.csFrameworkComponent1.MyNodeCollection.Add(this.csFrameworkNode4);
      this.csFrameworkComponent1.MyNodeCollection.Add(this.csFrameworkNode13);
      this.csFrameworkComponent1.MyNodeCollection.Add(this.csFrameworkNode14);
      this.csFrameworkComponent1.MyNodeCollection.Add(this.csFrameworkNode15);
      this.csFrameworkComponent1.MyNodeCollection.Add(this.csFrameworkNode16);
      //
      // csFrameworkNode2
      //
      this.csFrameworkNode2.ID = 1;
      this.csFrameworkNode2.NodeName = "名称:1";
      //
      // csFrameworkNode3
      //
      this.csFrameworkNode3.ID = 2;
      this.csFrameworkNode3.NodeName = "名称:2";
      //
      // csFrameworkNode5
      //
      this.csFrameworkNode5.ID = 3;
      this.csFrameworkNode5.NodeName = "名称:3";
      //
      // csFrameworkNode6
      //
      this.csFrameworkNode6.ID = 4;
      this.csFrameworkNode6.NodeName = "名称:4";
      //
      // csFrameworkNode7
      //
      this.csFrameworkNode7.ID = 5;
      this.csFrameworkNode7.NodeName = "名称:5";
      //
      // csFrameworkNode8
      //
      this.csFrameworkNode8.ID = 6;
      this.csFrameworkNode8.NodeName = "名称:6";
      //
      // csFrameworkNode9
      //
      this.csFrameworkNode9.ID = 7;
      this.csFrameworkNode9.NodeName = "名称:7";
      //
      // csFrameworkNode10
      //
      this.csFrameworkNode10.ID = 8;
      this.csFrameworkNode10.NodeName = "名称:8";
      //
      // csFrameworkNode11
      //
      this.csFrameworkNode11.ID = 9;
      this.csFrameworkNode11.NodeName = "名称:9";
      //
      // csFrameworkNode12
      //
      this.csFrameworkNode12.ID = 10;
      this.csFrameworkNode12.NodeName = "名称:10";
      //
      // csFrameworkNode1
      //
      this.csFrameworkNode1.ID = 470;
      this.csFrameworkNode1.NodeName = "www.CSFramework.com";
      //
      // csFrameworkNode4
      //
      this.csFrameworkNode4.ID = 307;
      this.csFrameworkNode4.NodeName = "www.CSFramework.com";
      //
      // csFrameworkNode13
      //
      this.csFrameworkNode13.ID = 0;
      this.csFrameworkNode13.NodeName = null;
      //
      // csFrameworkNode14
      //
      this.csFrameworkNode14.ID = 0;
      this.csFrameworkNode14.NodeName = null;
      //
      // csFrameworkNode15
      //
      this.csFrameworkNode15.ID = 0;
      this.csFrameworkNode15.NodeName = null;
      //
      // csFrameworkNode16
      //
      this.csFrameworkNode16.ID = 0;
      this.csFrameworkNode16.NodeName = null;
      //
      // Form1
      //
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(734, 381);
      this.Controls.Add(this.button1);
      this.Name = "Form1";
      this.Text = "Form1";
      this.ResumeLayout(false);
      
   }
   
   #endregion
   
   private CSFrameworkComponent csFrameworkComponent1;
   private CSFrameworkNode csFrameworkNode2;
   private CSFrameworkNode csFrameworkNode3;
   private CSFrameworkNode csFrameworkNode1;
   private CSFrameworkNode csFrameworkNode4;
   private System.Windows.Forms.Button button1;
   private CSFrameworkNode csFrameworkNode5;
   private CSFrameworkNode csFrameworkNode6;
   private CSFrameworkNode csFrameworkNode7;
   private CSFrameworkNode csFrameworkNode8;
   private CSFrameworkNode csFrameworkNode9;
   private CSFrameworkNode csFrameworkNode10;
   private CSFrameworkNode csFrameworkNode11;
   private CSFrameworkNode csFrameworkNode12;
   private CSFrameworkNode csFrameworkNode13;
   private CSFrameworkNode csFrameworkNode14;
   private CSFrameworkNode csFrameworkNode15;
   private CSFrameworkNode csFrameworkNode16;
   
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898





C/S框架网|原创精神.创造价值.打造精品


扫一扫加作者微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务


上一篇 下一篇