C#中TreeView智能选择节点

//加载数据到treeview
private void LoadfvFunData(Comm.IdTextTreeNode tv,string parent)
  {
   FunctionDTO[] fdto=wfm.findFunByModle(parent);
     if(fdto!=null)
     {
      foreach(FunctionDTO dto in fdto)
      {
       Comm.IdTextTreeNode idText=new IdTextTreeNode(dto.funcid,dto.funcname);
       if(tv!=null)
       {
        tv.Nodes.Add(idText);
       }
       else
       {
        tvFun.Nodes.Add(idText);
       }
       LoadfvFunData(idText,dto.funcid);//递归调用填充所有的treenode
      }
    }
  }
//在选中了一个功能之后要进行的操作,如果是子选项,则自动选中其父选项,如果还有子选项,则同时把其子项也选中。
  private void tvFun_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
  {
   if(e.Action != TreeViewAction.Unknown)//防止AfterCheck事件被多次引发并可能产生意外行为
   {
    this.CheckChild((Comm.IdTextTreeNode)e.Node);
    this.CheckParent((Comm.IdTextTreeNode)e.Node);
   }
  }
//把父NODE选中
  private void CheckParent(Comm.IdTextTreeNode itt)
  {
   if(itt.Parent!=null)
   {
    if(itt.Checked)
    {
     itt.Parent.Checked=itt.Checked;
     CheckParent((Comm.IdTextTreeNode)itt.Parent);
    }
    else
    {
     foreach(Comm.IdTextTreeNode tempitt in itt.Parent.Nodes)
     {
      if(tempitt.Checked)
      {
       itt.Parent.Checked=true;
       return;
      }
      else
      {
       itt.Parent.Checked=itt.Checked;
       CheckParent((Comm.IdTextTreeNode)itt.Parent);
      }
     }
    }
   }
  }
  //把所有子项也选中
  private void CheckChild(Comm.IdTextTreeNode itf)
  {
   if(itf.Nodes.Count>0)
   {
    foreach(Comm.IdTextTreeNode idttemp in itf.Nodes)
    {
     idttemp.Checked=itf.Checked;
     CheckChild(idttemp);
    }
   }
  }
//已经有的功能在treeview中显示选中
/// <summary>
  /// 确定当前已经有的功能CHECH显示
  /// </summary>
  private void  loadFunData()
  {
   FunctionDTO[] funCheck=wfm.findFunsByRole(this._dataId);
   if(funCheck!=null)
   {
    for(int i=0;i<funCheck.Length;i++)
    {
     foreach(Comm.IdTextTreeNode itn in tvFun.Nodes)
     {
      if(funCheck[i].funcid==itn.ID)
      {
       itn.Checked=true;
      }
      checkFun(itn,funCheck[i].funcid);
     }
    }
    
   }
   
  }
  private void checkFun(Comm.IdTextTreeNode itnote,string ComID)
  {
   if(itnote.Nodes!=null)
   {
    foreach(Comm.IdTextTreeNode itn in itnote.Nodes)
    {
     if(ComID==itn.ID)
     {
      itn.Checked=true;
     }
     checkFun(itn,ComID);
    }
   }
  }
posted @ 2007-04-11 00:23  随风而逝  阅读(2116)  评论(1)    收藏  举报