Treeview的一個遞迴演算法

來源:互聯網
上載者:User
Option   Explicit  
   
  Private   Sub   Form_Load()  
          With   TreeView1  
                  .Nodes.Add   ,   ,   "Parent1",   "Test1"  
                  .Nodes.Add   "Parent1",   tvwChild,   "Child1",   "Test_CH1"  
                  .Nodes.Add   "Parent1",   tvwChild,   "Child2",   "Test_CH2"  
                  .Nodes.Add   "Parent1",   tvwChild,   "Child3",   "Test_CH3"  
                  .Nodes.Add   "Child3",   tvwChild,   "Child_33",   "Test_CH3_CH3"  
                  .Nodes.Add   "Parent1",   tvwChild,   "Child4",   "Test_CH4"  
                  .Nodes.Add   "Parent1",   tvwChild,   "Parent2",   "Test2"  
                  .Nodes.Add   "Parent2",   tvwChild,   "Child11",   "Test_CH1"  
                  .Nodes.Add   "Parent2",   tvwChild,   "Child22",   "Test_CH2"  
                  .Nodes.Add   "Parent2",   tvwChild,   "Child33",   "Test_CH3"  
                  .Nodes.Add   "Parent2",   tvwChild,   "Child44",   "Test_CH4"  
                  .Checkboxes   =   True  
                  .LineStyle   =   tvwTreeLines  
          End   With  
          NodeAllExpanded   TreeView1  
  End   Sub  
   
   
  Private   Sub   TreeView1_NodeCheck(ByVal   Node   As   MSComctlLib.Node)  
          Call   AllChildSynchro(Node)  
          Call   AllMaterSynchro(Node)  
  End   Sub  
   
   
  '//   -選中下級-  
  Private   Sub   AllChildSynchro(ByVal   NodeChoose   As   Object)  
          On   Error   Resume   Next  
          Dim   lNextLoop   As   Long  
          Dim   ObjChildren   As   Object  
          If   CBool(NodeChoose.Children   >   0)   Then  
                  Set   ObjChildren   =   NodeChoose.Child  
                  For   lNextLoop   =   1   To   NodeChoose.Children  
                          ObjChildren.Checked   =   NodeChoose.Checked  
                          If   ObjChildren.Children   >   0   Then   Call   AllChildSynchro(ObjChildren)  
                          Set   ObjChildren   =   ObjChildren.Next  
                  Next   lNextLoop  
          End   If  
  End   Sub  
   
  '//   -取消上級-  
  Private   Sub   AllMaterSynchro(ByVal   NodeChoose   As   Object)  
          On   Error   Resume   Next  
          If   Not   NodeChoose.Checked   Then   If   TypeName(NodeChoose.Parent)   =   "INode"   Then   NodeChoose.Parent.Checked   =   False  
          If   Not   NodeChoose.Checked   Then   If   TypeName(NodeChoose.Parent)   =   "INode"   Then   Call   AllMaterSynchro(NodeChoose.Parent)  
  End   Sub  
   
  '//   -節點張開-  
  Private   Sub   NodeAllExpanded(ByVal   ObjTvw   As   Object)  
          On   Error   Resume   Next  
          Dim   ObjChild   As   Object  
          For   Each   ObjChild   In   ObjTvw.Nodes  
                  ObjChild.Expanded   =   IIf(ObjChild.Key   =   ObjChild.Root.Key,   False,   True)  
                  ObjChild.Root.Expanded   =   IIf(ObjChild.Key   =   ObjTvw.Nodes(ObjTvw.Nodes.Count).Key,   True,   False)  
          Next   ObjChild  
  End   Sub   
    
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.