根据合成模式 创建treeview

using System;
using System.Collections;

namespace   Base
{
 /// <summary>
 /// ModelBase 的摘要说明。
 /// 数据实体类的基类
 /// </summary>
 public class ModelBase_Beat: ArrayList
 {
  public ModelBase_Beat()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
 }

 

using System;
using System.Collections ;

 

 

namespace   Base

{


 /// <summary>
 /// Component
 /// 抽象类
 /// </summary>
 public abstract class ComponentSafe:IDisposable
 {
  #region Field
  /// <summary>
  /// 节点名称
  /// </summary>
  protected string _Caption = string.Empty;

  /// <summary>
  /// 节点索引
  /// </summary>
  protected string currentIndex = string.Empty;

  /// <summary>
  /// 数据类的基类
  /// 用于保存节点对应的数据表的信息
  /// </summary>
  protected ModelBase_Beat _ModelBase_Beat;

  #endregion

  #region NodeBase属性
  /// <summary>
  /// 属性 节点名称
  /// 用于获得,设置Tree中节点名称
  /// </summary>
  public virtual string Caption
  {
   set{ this._Caption = value ;}
   get{ return this._Caption; }
  }
  
  
  /// <summary>
  /// 属性 节点数据
  /// 用户获得、设置节点数据内容
  /// </summary>
  public virtual ModelBase_Beat Model
  {
   set{ _ModelBase_Beat = value; }
   get{ return _ModelBase_Beat; }
  }

  /// <summary>
  /// 属性 节点所在树位置索引
  /// [0][0][0] -> 0.0.0
  /// </summary>
  public string CurrentIndex
  {
   get { return currentIndex; }
   set { currentIndex = value; }
  }


  #endregion

  #region 构造函数

  /// <summary>
  /// 构造函数
  /// </summary>
  /// <param name="name">节点名称</param>
  public ComponentSafe( string name )
  { this._Caption = name; }
  
  #endregion
  
  #region 共有方法
  /// <summary>
  /// 得到节点名称
  /// </summary>
  /// <returns></returns>
  public string getCaption()
  {
   return _Caption;
  }
  
  /// <summary>
  /// 得到本节点数据
  /// </summary>
  /// <returns></returns>
  public ModelBase_Beat getModel()
  {
   return _ModelBase_Beat;
  }

  #endregion

  abstract public IEnumerator getSubordinates();

  #region 释放资源

  // Other managed resource this class uses.
  private Component component = new Component();

  // Pointer to an external unmanaged resource.
  private IntPtr handle;


  // Track whether Dispose has been called.
  private bool disposed = false;

  // Implement IDisposable.
  // Do not make this method virtual.
  // A derived class should not be able to override this method.
  public void Dispose()
  {
   Dispose(true);
   // This object will be cleaned up by the Dispose method.
   // Therefore, you should call GC.SupressFinalize to
   // take this object off the finalization queue
   // and prevent finalization code for this object
   // from executing a second time.
   GC.SuppressFinalize(this);
  }
  // Dispose(bool disposing) executes in two distinct scenarios.
  // If disposing equals true, the method has been called directly
  // or indirectly by a user's code. Managed and unmanaged resources
  // can be disposed.
  // If disposing equals false, the method has been called by the
  // runtime from inside the finalizer and you should not reference
  // other objects. Only unmanaged resources can be disposed.
  private void Dispose(bool disposing)
  {
   // Check to see if Dispose has already been called.
   if(!this.disposed)
   {
    // If disposing equals true, dispose all managed
    // and unmanaged resources.
    if(disposing)
    {
     // Dispose managed resources.
     component.Dispose();
    }
            
    // Call the appropriate methods to clean up
    // unmanaged resources here.
    // If disposing is false,
    // only the following code is executed.
    CloseHandle(handle);
    handle = IntPtr.Zero;           
   }
   disposed = true;        
  }
  // Use interop to call the method necessary 
  // to clean up the unmanaged resource.
  [System.Runtime.InteropServices.DllImport("Kernel32")]
  private extern static Boolean CloseHandle(IntPtr handle);

  // Use C# destructor syntax for finalization code.
  // This destructor will run only if the Dispose method
  // does not get called.
  // It gives your base class the opportunity to finalize.
  // Do not provide destructors in types derived from this class.
  ~ComponentSafe()     
  {
   // Do not re-create Dispose clean-up code here.
   // Calling Dispose(false) is optimal in terms of
   // readability and maintainability.
   Dispose(false);
  }
       
  #endregion
 }


 
 /// <summary>
 /// Composite
 /// 树枝
 /// </summary>
 public class CompositeSafe : ComponentSafe
 {
  private ArrayList children = new ArrayList();
  /// <summary>
  /// 构造函数
  /// </summary>
  /// <param name="name"></param>
  public CompositeSafe( string name ) : base( name ) {}
  /// <summary>
  /// 增加子项
  /// </summary>
  /// <param name="component"></param>
  public  void Add( ComponentSafe component )
  { children.Add( component ); }
  /// <summary>
  /// 删除子项
  /// </summary>
  /// <param name="component"></param>
  public  void Remove( ComponentSafe component )
  { children.Remove( component ); }
        /// <summary>
        /// 得到子项的枚举
        /// </summary>
        /// <returns></returns>
  public override IEnumerator getSubordinates()
  {
   return children.GetEnumerator ();
  }
 }
 

 /// <summary>
 /// Leaf
 /// 叶子
 /// </summary>
 public class LeafSafe : ComponentSafe
 {
  private ArrayList childLeaf = new ArrayList(1);
  /// <summary>
  /// 构造函数
  /// </summary>
  /// <param name="name"></param>
  public LeafSafe( string name ) : base( name ) {}
  public override IEnumerator getSubordinates()
  {
   return childLeaf.GetEnumerator();
  }
 }
   
 /// <summary>
 /// 树节点
 /// 继承TreeNode 添加自己的属性
 /// </summary>
 public class DisplayNodeSafe:System.Windows.Forms.TreeNode    
 {
  
  /// <summary>
  /// 树的抽象类
  /// </summary>
  private ComponentSafe cmpp;
  /// <summary>
  /// 节点的层次索引
  /// 0.0.1
  /// 0.1.2
  /// ... ...
  /// </summary>
  protected string _CurrentIndex = string.Empty;

  /// <summary>
  /// 节点索引层次属性
  /// </summary>
  public string CurrentIndex
  {
   set{ _CurrentIndex = value;}
   get{ return _CurrentIndex; }
  }

  /// <summary>
  /// 设置节点对象的数据
  /// </summary>
  /// <param name="cmp"></param>
  public DisplayNodeSafe(ComponentSafe cmp )
  {
   cmp.getCaption ();
   cmpp = cmp;   
   this.Text =cmp.getCaption ();
   this.Tag =cmp.getModel();
   this.CurrentIndex = cmp.CurrentIndex;
  }

 }
 

}

namespace   Base

{
 public class UItest

 {

  public UItest()
  {
   buildDisplayList();
   buildTree();
  }

  private CompositeSafe root;
  private void buildDisplayList()
           
  {
   root = new CompositeSafe("南京铁道职业技术学院");
   CompositeSafe rootxi=new CompositeSafe("通信工程系");
   root.Add(rootxi);
   CompositeSafe rootxi1=new CompositeSafe("计算机系");
   root.Add(rootxi1);
   CompositeSafe rootjywl=new CompositeSafe("网络教研室");
   CompositeSafe rootjytx=new CompositeSafe("通讯教研室");
   CompositeSafe rootjyyd=new CompositeSafe("移动通信教研室");
   rootxi.Add(rootjywl);
   rootxi.Add(rootjytx);
   rootxi.Add(rootjyyd);
           
   CompositeSafe rootjyrj=new CompositeSafe("软件");
   rootxi1.Add(rootjyrj);
   //           
   LeafSafe LeafT =  new LeafSafe("沈瑞琴");
   rootjywl.Add(LeafT);
   rootjywl.Add(new LeafSafe("晏荣"));
   rootjywl.Add(new LeafSafe("蒋明华"));
   rootjywl.Add(new LeafSafe("赵丽花"));
   rootjywl.Add(new LeafSafe("康瑞峰"));
   rootjywl.Add(new LeafSafe("冯明兵"));
   rootjywl.Add(new LeafSafe("刘伟"));
           
                   
  }
  private void buildTree()
       
  {
   DisplayNodeSafe nod;
 
   nod = new  DisplayNodeSafe(root);
   //treeView1.Nodes.Add(nod);
   addNodes(nod, root);
  }
  private void addNodes(DisplayNodeSafe nod, ComponentSafe cmp)
        
  {
   ComponentSafe newCmp;
   DisplayNodeSafe newNode;
   IEnumerator cmpEnum;
   cmpEnum = cmp.getSubordinates();
 
   while (cmpEnum.MoveNext())
           
   {
    newCmp = (ComponentSafe)cmpEnum.Current;
    newNode = new  DisplayNodeSafe(newCmp);
    nod.Nodes.Add(newNode);
    addNodes(newNode, newCmp);
   }
  }
 }

}

 

 

 

public StringUnit() { // //TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 生成随机数 /// </summary> /// <returns></returns> private string GenerateCheckCode() { #region int number; char code; string checkCode = String.Empty; System.Random random = new Random(); for (int i = 0; i < 5; i++) { number = random.Next(); if (number % 2 == 0) code = (char)('0' + (char)(number % 10)); else code = (char)('A' + (char)(number % 26)); checkCode += code.ToString(); } HttpContext.Current.Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); return checkCode; #endregion } /// <summary> /// 生成验证码图片 /// </summary> public void CreateCheckCodeImage() { #region string checkCode = GenerateCheckCode(); if (checkCode == null || checkCode.Trim() == String.Empty) return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的背景噪音线 for (int i = 0; i < 25; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); } Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString(checkCode, font, brush, 2, 2); //画图片的前景噪音点 for (int i = 0; i < 100; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //画图片的边框线 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ContentType = "image/Gif"; HttpContext.Current.Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } #endregion }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值