TreeView 操作应用

None.gif 1  在窗体Form_Load里面初始化TreeView
ExpandedBlockStart.gifContractedBlock.gif    
/**/ /**/ /**/ /// <summary>
InBlock.gif        
/// 初始化TreeView
ExpandedBlockEnd.gif        
/// </summary>

None.gif          private   void  InitTreeview()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                treeView1.CheckBoxes 
= true;
InBlock.gif                treeView1.Nodes.Clear();
InBlock.gif                dt_menu
=new DataTable("dt_rightTable");                
InBlock.gif                dt_menu.Columns.Add(
"ID"typeof(String));
InBlock.gif                dt_menu.Columns.Add(
"RightName"typeof(String));
InBlock.gif                dt_menu.Columns.Add(
"ParentID"typeof(String));    
InBlock.gif
InBlock.gif                DataRow dr;
ContractedSubBlock.gifExpandedSubBlockStart.gif                增加权限数据表
增加权限数据表#region 增加权限数据表
ContractedSubBlock.gifExpandedSubBlockStart.gif                资源管理
资源管理#region    资源管理
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="1";
InBlock.gif                dr[
"RightName"]="资源管理";
InBlock.gif                dr[
"ParentID"]="0";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="11";
InBlock.gif                dr[
"RightName"]="查询";
InBlock.gif                dr[
"ParentID"]="1";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="12";
InBlock.gif                dr[
"RightName"]="增加";
InBlock.gif                dr[
"ParentID"]="1";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="13";
InBlock.gif                dr[
"RightName"]="修改";
InBlock.gif                dr[
"ParentID"]="1";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="14";
InBlock.gif                dr[
"RightName"]="删除";
InBlock.gif                dr[
"ParentID"]="1";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif                资源中心
资源中心#region    资源中心
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="2";
InBlock.gif                dr[
"RightName"]="资源中心";
InBlock.gif                dr[
"ParentID"]="0";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="21";
InBlock.gif                dr[
"RightName"]="查询";
InBlock.gif                dr[
"ParentID"]="2";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="22";
InBlock.gif                dr[
"RightName"]="增加";
InBlock.gif                dr[
"ParentID"]="2";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
ExpandedSubBlockEnd.gif                
#endregion
            
ContractedSubBlock.gifExpandedSubBlockStart.gif                决策支持
决策支持#region 决策支持
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="3";
InBlock.gif                dr[
"RightName"]="决策支持";
InBlock.gif                dr[
"ParentID"]="0";
InBlock.gif                dt_menu.Rows.Add(dr);
ContractedSubBlock.gifExpandedSubBlockStart.gif                地图导航
地图导航#region    地图导航
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="31";
InBlock.gif                dr[
"RightName"]="地图导航";
InBlock.gif                dr[
"ParentID"]="3";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="311";
InBlock.gif                dr[
"RightName"]="编辑";
InBlock.gif                dr[
"ParentID"]="31";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="312";
InBlock.gif                dr[
"RightName"]="查询";
InBlock.gif                dr[
"ParentID"]="31";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                数据报表
数据报表#region 数据报表
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="32";
InBlock.gif                dr[
"RightName"]="数据报表";
InBlock.gif                dr[
"ParentID"]="3";
InBlock.gif                dt_menu.Rows.Add(dr);
ContractedSubBlock.gifExpandedSubBlockStart.gif                旅游参观人数统计
旅游参观人数统计#region    旅游参观人数统计
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="321";
InBlock.gif                dr[
"RightName"]="旅游参观人数统计";
InBlock.gif                dr[
"ParentID"]="32";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                社会无力维修文物专项补助经费统计表
社会无力维修文物专项补助经费统计表#region    社会无力维修文物专项补助经费统计表
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="322";
InBlock.gif                dr[
"RightName"]="社会无力维修文物专项补助经费统计表";
InBlock.gif                dr[
"ParentID"]="32";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                古村落、古建筑保护维修情况统计表
古村落、古建筑保护维修情况统计表#region    古村落、古建筑保护维修情况统计表
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="323";
InBlock.gif                dr[
"RightName"]="古村落、古建筑保护维修情况统计表";
InBlock.gif                dr[
"ParentID"]="32";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                文物保护专项经费使用情况报表
文物保护专项经费使用情况报表#region    文物保护专项经费使用情况报表
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="324";
InBlock.gif                dr[
"RightName"]="文物保护专项经费使用情况报表";
InBlock.gif                dr[
"ParentID"]="32";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                举报问题统计表
举报问题统计表#region    举报问题统计表
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="325";
InBlock.gif                dr[
"RightName"]="举报问题统计表";
InBlock.gif                dr[
"ParentID"]="32";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                发文管理
发文管理#region    发文管理
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="326";
InBlock.gif                dr[
"RightName"]="发文管理";
InBlock.gif                dr[
"ParentID"]="32";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                收文管理
收文管理#region    收文管理
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="327";
InBlock.gif                dr[
"RightName"]="收文管理";
InBlock.gif                dr[
"ParentID"]="32";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif                
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif                工作信息
工作信息#region  工作信息
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="4";
InBlock.gif                dr[
"RightName"]="工作信息";
InBlock.gif                dr[
"ParentID"]="0";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="41";
InBlock.gif                dr[
"RightName"]="查询";
InBlock.gif                dr[
"ParentID"]="4";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="42";
InBlock.gif                dr[
"RightName"]="增加";
InBlock.gif                dr[
"ParentID"]="4";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="43";
InBlock.gif                dr[
"RightName"]="修改";
InBlock.gif                dr[
"ParentID"]="4";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="44";
InBlock.gif                dr[
"RightName"]="删除";
InBlock.gif                dr[
"ParentID"]="4";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="45";
InBlock.gif                dr[
"RightName"]="摸板维护";
InBlock.gif                dr[
"ParentID"]="4";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif                
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif                工作计划
工作计划#region    工作计划
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="5";
InBlock.gif                dr[
"RightName"]="工作计划";
InBlock.gif                dr[
"ParentID"]="0";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="51";
InBlock.gif                dr[
"RightName"]="查询";
InBlock.gif                dr[
"ParentID"]="5";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="52";
InBlock.gif                dr[
"RightName"]="删除";
InBlock.gif                dr[
"ParentID"]="5";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif                工作动态
工作动态#region 工作动态
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="6";
InBlock.gif                dr[
"RightName"]="工作动态";
InBlock.gif                dr[
"ParentID"]="0";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="61";
InBlock.gif                dr[
"RightName"]="查询";
InBlock.gif                dr[
"ParentID"]="6";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="62";
InBlock.gif                dr[
"RightName"]="删除";
InBlock.gif                dr[
"ParentID"]="6";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif                科研项目
科研项目#region    科研项目
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="7";
InBlock.gif                dr[
"RightName"]="科研项目";
InBlock.gif                dr[
"ParentID"]="0";
InBlock.gif                dt_menu.Rows.Add(dr);
ContractedSubBlock.gifExpandedSubBlockStart.gif                建筑评估
建筑评估#region    建筑评估
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="71";
InBlock.gif                dr[
"RightName"]="建筑评估";
InBlock.gif                dr[
"ParentID"]="7";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="711";
InBlock.gif                dr[
"RightName"]="查看";
InBlock.gif                dr[
"ParentID"]="71";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="712";
InBlock.gif                dr[
"RightName"]="编辑";
InBlock.gif                dr[
"ParentID"]="71";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif                完好率评测
完好率评测#region   完好率评测
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="72";
InBlock.gif                dr[
"RightName"]="完好率评测";
InBlock.gif                dr[
"ParentID"]="7";
InBlock.gif                dt_menu.Rows.Add(dr);    
InBlock.gif            
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="721";
InBlock.gif                dr[
"RightName"]="查看";
InBlock.gif                dr[
"ParentID"]="72";
InBlock.gif                dt_menu.Rows.Add(dr);    
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="722";
InBlock.gif                dr[
"RightName"]="编辑";
InBlock.gif                dr[
"ParentID"]="72";
InBlock.gif                dt_menu.Rows.Add(dr);    
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif                
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif                文保档案
文保档案#region    文保档案
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="8";
InBlock.gif                dr[
"RightName"]="文保档案";
InBlock.gif                dr[
"ParentID"]="0";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                记录档案
记录档案#region    记录档案
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="81";
InBlock.gif                dr[
"RightName"]="记录档案";
InBlock.gif                dr[
"ParentID"]="8";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="811";
InBlock.gif                dr[
"RightName"]="查看";
InBlock.gif                dr[
"ParentID"]="81";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="812";
InBlock.gif                dr[
"RightName"]="维护";
InBlock.gif                dr[
"ParentID"]="81";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="813";
InBlock.gif                dr[
"RightName"]="删除";
InBlock.gif                dr[
"ParentID"]="81";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="814";
InBlock.gif                dr[
"RightName"]="摸板管理";
InBlock.gif                dr[
"ParentID"]="81";
InBlock.gif                dt_menu.Rows.Add(dr);
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                台帐管理
台帐管理#region 台帐管理
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="82";
InBlock.gif                dr[
"RightName"]="台帐管理";
InBlock.gif                dr[
"ParentID"]="8";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="821";
InBlock.gif                dr[
"RightName"]="查看";
InBlock.gif                dr[
"ParentID"]="82";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="822";
InBlock.gif                dr[
"RightName"]="维护";
InBlock.gif                dr[
"ParentID"]="82";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="823";
InBlock.gif                dr[
"RightName"]="删除";
InBlock.gif                dr[
"ParentID"]="82";
InBlock.gif                dt_menu.Rows.Add(dr);
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif                
dot.gif#region    
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
ExpandedSubBlockEnd.gif                
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif                系统管理
系统管理#region 系统管理
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="9";
InBlock.gif                dr[
"RightName"]="系统管理";
InBlock.gif                dr[
"ParentID"]="0";
InBlock.gif                dt_menu.Rows.Add(dr);    
InBlock.gif            
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="91";
InBlock.gif                dr[
"RightName"]="查看";
InBlock.gif                dr[
"ParentID"]="9";
InBlock.gif                dt_menu.Rows.Add(dr);    
InBlock.gif
InBlock.gif                dr
=dt_menu.NewRow();            
InBlock.gif                dr[
"ID"]="92";
InBlock.gif                dr[
"RightName"]="编辑";
InBlock.gif                dr[
"ParentID"]="9";
InBlock.gif                dt_menu.Rows.Add(dr);    
ExpandedSubBlockEnd.gif                
#endregion

ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif                AddTree(
0, (TreeNode)null);
InBlock.gif                             
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception exc)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(exc.Message);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif其中用到方法:
ExpandedBlockStart.gifContractedBlock.gif    
/**/ /**/ /**/ /// <summary>
InBlock.gif        
///  递归添加树的节点
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ParentID">父节点ID</param>
ExpandedBlockEnd.gif        
/// <param name="pNode">节点</param>

None.gif          public   void  AddTree( int  ParentID,TreeNode pNode) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            DataView dvTree 
= new DataView(dt_menu);
InBlock.gif            dvTree.RowFilter 
=  "[ParentID] = " + ParentID;
InBlock.gif            
foreach(DataRowView Row in dvTree) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                TreeNode Node
=new TreeNode() ;
InBlock.gif
InBlock.gif                TreeNode node_temp
=new TreeNode();
InBlock.gif
InBlock.gif                
if(pNode == null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//增加根节点
InBlock.gif
                    node_temp.Text =Row["RightName"].ToString();
InBlock.gif                    node_temp.Tag 
=Row["ID"].ToString();
InBlock.gif                    treeView1.Nodes.Add(node_temp);
InBlock.gif                    AddTree(Int32.Parse(Row[
"ID"].ToString()),node_temp);    //再次递归
ExpandedSubBlockEnd.gif
                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    node_temp.Text 
=Row["RightName"].ToString();
InBlock.gif                    node_temp.Tag 
=Row["ID"].ToString();
InBlock.gif                    pNode.Nodes.Add(node_temp);
InBlock.gif                    AddTree(Int32.Parse(Row[
"ID"].ToString()),node_temp);    //再次递归
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }
            
ExpandedBlockEnd.gif        }

None.gif
None.gif在TreeView的AfterCheck事件里面编写如下代码:
None.gif    
private   void  treeView1_AfterCheck( object  sender, System.Windows.Forms.TreeViewEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//单击了treeView控件所触发的事件
InBlock.gif            
//MessageBox.Show(e.Node.Index.ToString());
InBlock.gif            
//如果夫节点下面有子节点则全部选中
InBlock.gif
            if(e.Action != TreeViewAction.Unknown)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(e.Node.Nodes.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.CheckAllChildNodes(e.Node, e.Node.Checked);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
//如果选中子节点同时选中父节点
InBlock.gif
                if(e.Node.Checked == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.selectParent(e.Node);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
//如果取消子选择子节点,而且当所有本及子节点未选中时,取消父级节点
InBlock.gif
                if(e.Node.Checked == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.cancelParent(e.Node);
InBlock.gif                
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
                
ExpandedBlockEnd.gif        }

None.gif
None.gif使用到的方法:
None.gif    
public   void  CheckAllChildNodes(TreeNode treeNode,  bool  nodeChecked)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
foreach(TreeNode node in treeNode.Nodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                node.Checked 
= nodeChecked;
InBlock.gif                
if(node.Nodes.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.CheckAllChildNodes(node, nodeChecked);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif        
// 取消上级节点
None.gif
         private   void  cancelParent(TreeNode node)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if(node.Parent != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
bool isCheck = false;
InBlock.gif                
foreach(TreeNode n in node.Parent.Nodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(n.Checked == true)
InBlock.gif                        isCheck 
= true;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if(!isCheck)
InBlock.gif                    node.Parent.Checked 
= false;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif        
None.gif        
// 设置选中父节点
None.gif
         private   void  selectParent(TreeNode node)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if(node.Parent != null && node.Parent.Checked == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                node.Parent.Checked 
= true;
InBlock.gif                
if(node.Parent.Parent != null)
InBlock.gif                    
this.selectParent(node.Parent);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif取消TreeView里面所有的钩
None.gif    
private   void  simpleButton4_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            System.Windows.Forms.TreeNodeCollection nodes
=treeView1.Nodes;
InBlock.gif            ClearSelectTree(nodes);
ExpandedBlockEnd.gif        }
     // 清除所有打勾选项
None.gif
         private   void  ClearSelectTree(System.Windows.Forms.TreeNodeCollection nodes)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {            
InBlock.gif            
foreach(System.Windows.Forms.TreeNode node in nodes) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{  
InBlock.gif                node.Checked 
=false;                
InBlock.gif                ClearSelectTree(node.Nodes); 
//递归查找                 
ExpandedSubBlockEnd.gif
            }
 
ExpandedBlockEnd.gif        }

None.gif给每个TreeView里面节点的Tag加str_right  这里str_right是全局变量
None.gif            GetAllNode(treeView1.Nodes);遍历所有的节点:
ExpandedBlockStart.gifContractedBlock.gif    
/**/ /**/ /**/ /// <summary>
InBlock.gif        
/// 遍历treeview  调用GetAllNode(treeView1.Nodes)
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name="nodes"></param>

None.gif          private   void  GetAllNode(System.Windows.Forms.TreeNodeCollection nodes) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
InBlock.gif            
foreach(System.Windows.Forms.TreeNode node in nodes) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{  
InBlock.gif                
if (node.Checked)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (str_right!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        str_right
+=";"+node.Tag.ToString();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        str_right
=node.Tag.ToString();
ExpandedSubBlockEnd.gif                    }
                    
ExpandedSubBlockEnd.gif                }

InBlock.gif                GetAllNode(node.Nodes); 
//递归查找 
InBlock.gif
                
ExpandedSubBlockEnd.gif            }
 
ExpandedBlockEnd.gif        }
 
None.gif下面判断是否打钩
None.giftreeView1.Nodes[Convert.ToInt32(ch_array[
0 ].ToString()) - 1 ].Nodes[Convert.ToInt32(ch_array[ 1 ].ToString()) - 1 ].Checked = true ;

转载于:https://www.cnblogs.com/aaliujing/archive/2006/12/18/595967.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值