树状图 TreeView控件--添加/删除节点(亲自实践)

来源:互联网 发布:数据分析师证书被取消 编辑:程序博客网 时间:2024/05/16 04:00

新增节点
        '设定树状图中节点
        Dim node As TreeNode

        'Method   1:   straightforward   adding   of   nodes  
        With Me.TreeView1.Nodes
            'add   text  
            .Add("AddByText ")
            'since   with..end   with   is   used:   read   TreeView1.Nodes.Add   ....  

            'every   add   method   returns   the   newly   created   node.   You   can   use  
            'this   concept   set   the   result   to   a   variable   or   to   directly   add  
            'a   childnode:  
            .Add("AddByText2 ").Nodes.Add("ChildOfAddByText ")

            'this,   you   can   take   as   far   as   you   want  
            .Add("AddByText3 ").Nodes.Add("ChildOfAddByText ").Nodes.Add("Another   child ")

            '--  
            node = .Add("AddByText,   Attach   To   Variable ")
            node.Nodes.Add("Child   one ")
            node.Nodes.Add("Child   two ")
            '   --  
            With .Add("AddByText   Use   WithTo   Add   ChildNodes ").Nodes
                .Add("Child   1 ")
                .Add("Child   2 ")
                .Add("Child   3 ").Nodes.Add("Subchild   1 ")
            End With
        End With

 

复制节点    
        '设定树状图中节点    
        Dim node As TreeNode    
    
        'Method   2:   adding   by   node       
        'Like   virtually   every   .Net   method   you   can   directly   assign   an   object:       
        Me.TreeView1.Nodes.Add(New TreeNode("AddByNode1 "))    
        Me.TreeView1.Nodes.Add("AddByNode2 ").Nodes.Add("test1")    
    
        'check   out   the   overloading   possibilities   of   using   New()       
        'Another   advantage   of   this   method   is   that   you   can   add   a   complete   branch.       
        '(node   is   already   declared   as   TreeNode   above)       
        node = New TreeNode("MainNodeToAdd ")    
        node.Nodes.Add("Child   1 ")    
        node.Nodes.Add("Child   2 ")    
    
        'you   can   for   instance   add   this   newly   created   node   to   all   main   branches:       
        Dim enumNode As TreeNode    
        For Each enumNode In TreeView1.Nodes    
            enumNode.Nodes.Add(node.Clone)   ' <-   the   clone()   method   is   needed       
        Next    
    
说明:    
 红色字体部分,是将某个节点复制到指定节点之下.   
 注意,由于for循环的对象是TreeView1.Nodes,而节点"test1"的隶属关系是"AddByNode2".Nodes   
 所以节点"test1"没有被复制   


追加节点     
        ''Adding   will   always   add   the   the   node   at   the   end   of   the   collection.        
        ''Of   course   you   can   also   insert   at   a   specified   location:        
        Me.TreeView1.Nodes.Insert(2, New TreeNode("I   am   inserted   at   the   3th   position "))     
     
说明:     
 insert()函数中,第一个参数是从0 开始    
 所以上面方法三中第一个参数设定是2,而结果显示该节点插入位置为第三项    

删除节点及其下所有内容  
        '设定树状图中节点  
        Dim node As TreeNode  
  
        ''removing   is   done   much   in   the   same   way:     
        TreeView1.Nodes.Add("I   need   to   be   removed ").Nodes.Add("and   all   children   too ")  
        TreeView1.Nodes(0).Name = "test"  
  
        node = TreeView1.Nodes("test")  
        node.Remove()  
  
  
说明:  
 注意,使用TreeView1.Nodes()定位节点的时候,有2种方法, 
 第一种,参数是Index,从0开始 
 第二种,参数是name,而不是text. 
 上例中如果写作TreeView1.Nodes("I   need   to   be   removed "),则会系统报错,Nodes返回值为Nothing 

删除前:

删除后:

 

删除节点其下的所有内容(该节点保留)  
        '设定树状图中节点  
        Dim node As TreeNode  
  
        ''removing   is   done   much   in   the   same   way:     
        TreeView1.Nodes.Add("I   need   to   be   removed ").Nodes.Add("and   all   children   too ")  
        TreeView1.Nodes(0).Name = "test"  
  
        node = TreeView1.Nodes("test")  
        node.Nodes.Clear()  
  
  
说明:  
 clear()方法清除的是指定节点下所属的内容,而该指定节点不受影响 

删除前:


删除后:


 

展开/关闭节点  
        'the   behaviour   of   the   treenode   can   be   controlled   completely   in   code.     
        'you   can   make   it   expand     
        TreeView1.Nodes(0).Expand()  
  
        ''and   retract   again     
        TreeView1.Nodes(0).Collapse()  

原创粉丝点击