用心快乐...

用心才能找到快乐...

重写的树控件

namespace  MyTree.Controls
{
 /// <summary>
 /// 作用:重写树控件
 ///  编写日期:2004-7-2
 /// </summary>
 ///
 public class Tree : System.Windows .Forms .TreeView
 {
  private string[] _ParentFields;
  private string[] _DisplayFields;
  private string[] _ValueFields;
  private string[] _SortOrder;
  /// <summary>
  /// 定义WebService
  /// </summary>
  public XSTBD.BDWSCall.WebSrvQuery.WebSrvQuery  webSrvQuery ;
  
  /// <summary>
  /// 定义属性 DisPlayFields:保存树节点所显示的字段名称
  /// </summary>
  public string[] DisplayFields
  {
   get
   {
    return this._DisplayFields;
   }
   set
   {
    if (this._DisplayFields != value)
     this._DisplayFields = value;
   }
  }
  /// <summary>
  /// 定义属性 ValueFields:保存树节点关联数据字段,与属性ParentFields一起确定父子节点之间的关系
  /// </summary>
  public string[] ValueFields
  {
   get
   {
    return this._ValueFields;
   }
   set
   {
    if (this._ValueFields != value)
     this._ValueFields = value;
   }
  }

  /// <summary>
  /// 定义属性 ParentFields:保存父节点关联数据子段,与属性ValueFields一起确定父子节点之间的关系
  /// </summary>
  public string[] ParentFields
  {
   get
   {
    return this._ParentFields;
   }
   set
   {
    if (this._ParentFields != value)
     this._ParentFields = value;
   }
  }
  /// <summary>
  /// 定义属性 SortOrder:树层排序字段
  /// </summary>
  public string[] SortOrder
  {
   get
   {
    return this._SortOrder ;
   }
   set
   {
    if (this._SortOrder != value)
     this._SortOrder = value;
   }
  }
  /// <summary>
  /// 定义缺省构造函数
  /// </summary>
  public Tree()
  { 

  }
  /// <summary>
  /// 添加节点
  /// </summary>
  /// <param name="PNode">父节点</param>
  /// <param name="planNodeInfo">节点信息实体</param>
  public void AddNode(TreeNode PNode,TreeNodeInfo planNodeInfo)
  {  
   TreeNode node = new TreeNode();
   node.Text  = planNodeInfo.nodeText;
   node.Tag = planNodeInfo;
   node.ImageIndex  = planNodeInfo.level;
   node.SelectedImageIndex = node.ImageIndex;
   PNode.Nodes.Add(node);
  }

  /// <summary>
  /// 添加节点
  /// </summary>
  /// <param name="PNode">父节点</param>
  /// <param name="planNodeInfo">节点信息实体</param>
  /// <param name="imageIndex">图标序号</param>
  /// <returns>返回节点索引</returns>
  public int AddNode(TreeNode PNode,TreeNodeInfo planNodeInfo,int imageIndex)
  {  
   TreeNode node = new TreeNode();
   node.Text  = planNodeInfo.nodeText;
   node.Tag = planNodeInfo;
   node.ImageIndex  = imageIndex;
   node.SelectedImageIndex = node.ImageIndex;
   return PNode.Nodes.Add(node);
  }
  /// <summary>
  /// 修改节点信息
  /// </summary>
  /// <param name="node">所修改的节点</param>
  /// <param name="planNodeInfo">节点信息实体</param>
  public void UpdateNode(TreeNode node,TreeNodeInfo planNodeInfo)
  {  
   node.Text = planNodeInfo.nodeText ;
   node.Tag = planNodeInfo;
  }
  /// <summary>
  /// 刷新节点图标
  /// </summary>
  /// <param name="node">所刷新节点</param>
  /// <param name="imageIndex">图标序号</param>
  public void RefurbishImage(TreeNode node,int imageIndex)
  {
   node.ImageIndex = imageIndex;
   node.SelectedImageIndex = node.ImageIndex ;
  }
  /// <summary>
  /// 删除节点及其所有子节点
  /// </summary>
  /// <param name="Node">删除节点名称</param>
  public void DelNode(TreeNode Node)
  {  
   if (Node ==null) return;
   Node.Remove ();
  }
  /// <summary>
  /// 显示树
  /// </summary>
  /// <param name="PNode">父节点,可以为空。如果不为空,将树作为其子树在其下显示;否则作为一棵新树显示</param>
  /// <param name="Data">从数据集获得节点显示数据</param>
  /// <param name="Height">待加载树的层数</param>
  public void DrawTree(TreeNode  PNode,DataSet Data,int Height)
  {   
   if (PNode == null)
   {
    DataView dataView = new DataView() ;
    dataView.Table = Data.Tables[0];
    string rowFilter;
    rowFilter = this._ValueFields[0] + " >1 " ;
    rowFilter = rowFilter + " and " + this._ValueFields[1] +"=0";
    dataView.RowFilter = rowFilter; 
    for (int i = 0;i < dataView.Count;i++ )
    {
     TreeNode  node = new TreeNode () ;
     node.Text  = dataView[i][this._DisplayFields[0]].ToString ();
     TreeNodeInfo nodeInfo = new TreeNodeInfo ();
     nodeInfo.nodeText = node.Text;
     nodeInfo.dataValue = System.Convert .ToInt32 (dataView[i][this._ValueFields[0]]);
     nodeInfo.level = ((TreeNodeInfo)PNode.Tag).level + 1;
     node.Tag = nodeInfo;
     node.ImageIndex = nodeInfo.level;
     node.SelectedImageIndex = node.ImageIndex;
     PNode.Nodes.Add(node);
     DrawNodes(node,Data,1,Height-1);
    }
   }
   else
   {
    DrawNodes(PNode,Data,1,Height);
    
   }
  }
  /// <summary>
  /// 显示树
  /// </summary>
  /// <param name="PNode">父节点,可以为空。如果不为空,将树作为其子树在其下显示;否则作为一棵新树显示</param>
  /// <param name="SelectStr">从查询字符串数组获得树节点显示的数据</param>
  /// <param name="Height">待加载树的层数</param>
  public void DrawTree(TreeNode  PNode,string[] SelectStr,int Height)
  {   
   if (PNode == null)
   {
    DataSet dataSet = webSrvQuery.GetQueryDataSetBySQLText(SelectStr[0]);
    foreach (DataRow dataRow in dataSet.Tables[0].Rows)
    {
     TreeNode  node = new TreeNode () ;
     node.Text  = dataRow[this._DisplayFields[0]].ToString ();
     TreeNodeInfo nodeInfo = new TreeNodeInfo ();
     nodeInfo.nodeText = node.Text;
     nodeInfo.dataValue = System.Convert .ToInt32 (dataRow[this._ValueFields[0]]);
     nodeInfo.level = ((TreeNodeInfo)PNode.Tag).level + 1;
     node.Tag = nodeInfo;
     if (((TreeNodeInfo)node.Tag).level >= 3)
      node.ImageIndex = 3 + System.Convert .ToInt32 (dataRow["DataType_NR"]);
     else
      node.ImageIndex = nodeInfo.level;
     node.SelectedImageIndex = node.ImageIndex;
     this.Nodes.Add(node);
     DrawNodes(node,SelectStr,1,Height-1);
    }
   }
   else
   {
    DrawNodes(PNode,SelectStr,1,Height);
    
   }
  }
       
  /// <summary>
  /// 显示树
  /// </summary>
  /// <param name="PNode">父节点,可以为空</param>
  /// <param name="SelectStr">查询字符串</param>
  /// <param name="Height">待加载树的层数</param>
  public void DrawTree(TreeNode  PNode,string SelectStr,int Height)
  {  
   DataSet data = webSrvQuery.GetQueryDataSetBySQLText (SelectStr);;
   DrawTree(PNode,data ,Height);
  }

  /// <summary>
  /// 显示子树节点
  /// </summary>
  /// <param name="PNode">显示子树的父节点,不能为空</param>
  /// <param name="Data">从数据集获得节点显示的数据</param>
  /// <param name="TreeHeight">当前树的高度</param>
  /// <param name="Height">待添加层数</param>
  public void DrawNodes(TreeNode PNode,DataSet Data,int TreeHeight,int Height)
  {  
   if ((Height <= 0)||(TreeHeight > this._ParentFields.Length))
   {
    return;
   }
   if (PNode.Nodes.Count > 0)    //节点已经存在
   {
    foreach (TreeNode node in PNode.Nodes)
     DrawNodes(node,Data,TreeHeight + 1,Height-1); 
   }
   else
   {
    DataView dataView = new DataView() ;
    dataView.Table = Data.Tables[0];
    string rowFilter;
    rowFilter = this._ValueFields[TreeHeight - 1] + " > 1 " ;
    if (TreeHeight <= 1)
    {
     rowFilter = rowFilter + " and " + this._ParentFields[TreeHeight - 1] +" =0 ";
    }
    if ( !((TreeNodeInfo)PNode.Tag).dataValue.Equals(0))
    {
     rowFilter = rowFilter + " and " + this._ParentFields[TreeHeight - 1] + "=" +
      ((TreeNodeInfo)PNode.Tag).dataValue.ToString() ;
    }
    dataView.RowFilter = rowFilter; 
    for (int i = 0;i < dataView.Count;i++ )
    {
     TreeNode  node = new TreeNode ();
     node.Text  = dataView[i][this._DisplayFields[TreeHeight - 1]].ToString ();
     TreeNodeInfo nodeInfo = new TreeNodeInfo ();
     nodeInfo.nodeText = node.Text;
     nodeInfo.dataValue = System.Convert.ToInt32(dataView[i][this._ValueFields[TreeHeight - 1]]);
     nodeInfo.level = ((TreeNodeInfo)PNode.Tag).level + 1;
     node.Tag = nodeInfo;
     node.ImageIndex = nodeInfo.level;
     node.SelectedImageIndex = node.ImageIndex;
     node.SelectedImageIndex = nodeInfo.level;
     PNode.Nodes.Add(node);
     DrawNodes(node,Data,TreeHeight + 1,Height-1);

    }
   }
  }
  /// <summary>
  /// 显示子树节点
  /// </summary>
  /// <param name="PNode">显示子树的父节点,不能为空</param>
  /// <param name="SelectStr">从查询字符串数组获得树节点显示数据</param>
  /// <param name="TreeHeight">当前树的高度</param>
  /// <param name="Height">待添加层数</param>
  public void DrawNodes(TreeNode  PNode,string[] SelectStr,int TreeHeight,int Height)
  {
   
   if ((Height <= 0)||(TreeHeight > this._ParentFields.Length))
   {
    return;
   }
   if (PNode.Nodes.Count >0)    //节点已经存在
   {
    foreach (TreeNode  node in PNode.Nodes)
     DrawNodes(node,SelectStr,TreeHeight + 1,Height - 1);    
   }
   else
   {
    string whereStr = "";
    if (!((TreeNodeInfo)PNode.Tag).dataValue.Equals(0))
    {
     string fieldName = this._ParentFields[TreeHeight - 1];
     if (fieldName.Length > 0)
      whereStr = whereStr + " and " + fieldName + "=" + ((TreeNodeInfo)PNode.Tag ).dataValue .ToString ();
    }
    
    DataSet dataSet = webSrvQuery.GetQueryDataSetBySQLText (SelectStr[TreeHeight - 1] + whereStr +
     " Order By " + SortOrder[TreeHeight - 1]); 
    foreach (DataRow dataRow in dataSet.Tables[0].Rows)
    { 
     TreeNode  node = new TreeNode ();
     node.Text  = dataRow[this._DisplayFields[TreeHeight - 1]].ToString ();
     TreeNodeInfo nodeInfo = new TreeNodeInfo ();
     nodeInfo.nodeText = node.Text;
     nodeInfo.dataValue = System.Convert .ToInt32 (dataRow[this._ValueFields[TreeHeight - 1]]);
     nodeInfo.level = ((TreeNodeInfo)PNode.Tag).level + 1;
     node.Tag = nodeInfo;
     if (this.Name == "planTree" && nodeInfo.level >= 3)
      node.ImageIndex = 3 + System.Convert .ToInt32 (dataRow["DataType_NR"]);
     else
      node.ImageIndex = nodeInfo.level;
     node.SelectedImageIndex = node.ImageIndex;
     PNode.Nodes.Add(node);
     DrawNodes(node,SelectStr,TreeHeight + 1,Height - 1);

    }
   }
  }
  /// <summary>
  /// 在PNode下加载树节点(根据查询字符串SelectStr获得子节点信息)
  /// </summary>
  /// <param name="PNode">父节点</param>
  /// <param name="SelectStr">查询字符串</param>
  /// <param name="DisplayStr">节点显示文本字段</param>
  /// <param name="ValueText">节点值字段</param>
  public void DrawNodes(TreeNode PNode,string SelectStr,string DisplayStr,string ValueText)
  {
   if (PNode.Nodes.Count > 0)
    return;
   DataSet dataSet = webSrvQuery.GetQueryDataSetBySQLText(SelectStr);
   foreach (DataRow dataRow in dataSet.Tables[0].Rows)
   {
    TreeNode  node = new TreeNode ();
    node.Text  = dataRow[DisplayStr].ToString ();
    TreeNodeInfo nodeInfo = new TreeNodeInfo ();
    nodeInfo.nodeText = node.Text;
    nodeInfo.dataValue = System.Convert .ToInt32 (dataRow[ValueText]);
    nodeInfo.level = ((TreeNodeInfo)PNode.Tag).level + 1;
    node.Tag = nodeInfo;
    node.ImageIndex = nodeInfo.level;
    node.SelectedImageIndex = node.ImageIndex;
    PNode.Nodes.Add(node);
   }
  }
  /// <summary>
  /// 显示子树节点
  /// </summary>
  /// <param name="PNode">显示子树的父节点,不能为空</param>
  /// <param name="SelectStr">从查询字符串获得节点显示的数据</param>
  /// <param name="TreeHeight">当前树的高度</param>
  /// <param name="Height">待添加层数</param>
  public void DrawNodes(TreeNode  PNode,string SelectStr,int TreeHeight,int Height)
  {  
   DataSet data = webSrvQuery.GetQueryDataSetBySQLText (SelectStr);
   DrawNodes(PNode,data,TreeHeight,Height);
  }
        /// <summary>
        /// 搜索节点
        /// </summary>
        /// <param name="PNode">父节点</param>
        /// <param name="SerchValue">搜索节点所在树路径各层值的数组</param>
        /// <param name="SelectStr">树各层查询字符串数组</param>
  public void SerchNode(TreeNode PNode,int[] SerchValue,string[] SelectStr)
  {  
   if (SerchValue.Length <= ((TreeNodeInfo)PNode.Tag).level + 1)
    return;
   foreach ( TreeNode node in PNode.Nodes )
   {
    if (((TreeNodeInfo)node.Tag).dataValue.Equals(SerchValue[((TreeNodeInfo)node.Tag).level]))
    {
     this.SelectedNode = node;
     SerchNode(node,SerchValue,SelectStr);
     break;
    }
       
   }
  }
        /// <summary>
        /// 搜索节点
        /// </summary>
        /// <param name="SerchValue">搜索节点所在树路径各层值的数组</param>
        /// <param name="SelectStr">树各层查询字符串数组</param>
  public void SerchNode(int[] SerchValue,string[] SelectStr)
  {  
   if (SerchValue.Length <= 0)
    return;
   foreach (TreeNode node in this.Nodes)
   {
    if (((TreeNodeInfo)node.Tag).dataValue.Equals(SerchValue[((TreeNodeInfo)node.Tag).level]))
    {  
     this.SelectedNode = node;
     SerchNode(node,SerchValue,SelectStr);
     break;
    }
   } 
  }
  /// <summary>
  /// 加载一层文本节点
  /// </summary>
  /// <param name="pNode">要加载的文本节点的父节点</param>
  /// <param name="textStr">要加载的节点集的文本的字符串组</param>
  public void DrawTextNodes(TreeNode  pNode,string[] textStr)
  {   
   if (pNode.Nodes.Count >0)    //节点已经存在
   {
    return;
   }
   int i = 0;

   foreach (string text in textStr)
   {

    TreeNode  node = new TreeNode () ;
    node.Text  = text;
    TreeNodeInfo nodeInfo = new TreeNodeInfo ();
    nodeInfo.nodeText = node.Text;
    nodeInfo.dataValue = i;
    nodeInfo.level = ((TreeNodeInfo)pNode.Tag).level + 1;
    node.Tag = nodeInfo;
    node.ImageIndex = i + 2;
    node.SelectedImageIndex = node.ImageIndex;
    pNode.Nodes.Add(node);
   
    i++;

   }

  }
  /// <summary>
  /// 加载子树节点(跨层获取关联的dataValue)
  /// </summary>
  /// <param name="PNode">加载子树的父节点,不能为空</param>
  /// <param name="SelectStr">通过查询字符串数组读数据库获得树节点显示数据</param>
  /// <param name="TreeHeight">当前树的高度</param>
  public void DrawNodesCross(TreeNode  PNode,string[] SelectStr,int TreeHeight)
  {
   if (PNode.Nodes.Count >0)    //节点已经存在
   {
    return;
   }
   string whereStr = "";
   string fieldName = this._ParentFields[TreeHeight - 1];

   //取父节点的父节点的dataValue
   whereStr = whereStr + " and " + fieldName + "=" + ((TreeNodeInfo)PNode.Parent.Tag ).dataValue .ToString ();
   
   DataSet dataSet = webSrvQuery.GetQueryDataSetBySQLText (SelectStr[TreeHeight - 1] + whereStr +
    " Order By " + SortOrder[TreeHeight - 1]); 
   foreach (DataRow dataRow in dataSet.Tables[0].Rows)
   { 
    TreeNode  node = new TreeNode ();
    node.Text  = dataRow[this._DisplayFields[TreeHeight - 1]].ToString ();
    TreeNodeInfo nodeInfo = new TreeNodeInfo ();
    nodeInfo.nodeText = node.Text;
    nodeInfo.dataValue = System.Convert .ToInt32 (dataRow[this._ValueFields[TreeHeight - 1]]);
    nodeInfo.level = ((TreeNodeInfo)PNode.Tag).level + 1;
    nodeInfo.nodeText = node.Text;
    node.Tag = nodeInfo;
    node.ImageIndex = nodeInfo.level - 1;//因为上一层的图标不与树同一系列
    node.SelectedImageIndex = node.ImageIndex;
    PNode.Nodes.Add(node);

   }
   
  }


 }

}

阅读更多
上一篇串口通信过程中更改波特率的方法 C#实现
下一篇Oracle 学习1
想对作者说点什么? 我来说一句

CTreeCtrl控件重绘总汇

2011年02月25日 4.85MB 下载

IE树控件IE树控件IE树控件

2008年02月22日 247KB 下载

控件的使用

2012年02月11日 990KB 下载

IDL文档树,IDL文档树控件创建

2010年05月25日 2KB 下载

C++做的一个树控件

2011年05月18日 20KB 下载

下拉框树控件(下拉框和树结合)

2011年03月11日 108KB 下载

没有更多推荐了,返回首页

关闭
关闭