怎么样根据合成模式创建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>
publicModelBase_BeatgetModel()
{
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>
publicstringCurrentIndex
{
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
{
publicUItest()
{
buildDisplayList();
buildTree();
}
privateCompositeSafe 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);
}
}
}
}
本文地址:http://www.45fan.com/a/luyou/72266.html