⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 Visual.Basic.NET实用编程百例-47.6M.zip
💻 FRM
字号:
VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form Form1 
   Caption         =   "使用Treeview"
   ClientHeight    =   4350
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6840
   LinkTopic       =   "Form1"
   ScaleHeight     =   4350
   ScaleWidth      =   6840
   StartUpPosition =   3  'Windows Default
   Begin VB.DirListBox Dir1 
      Height          =   510
      Left            =   1320
      TabIndex        =   1
      Top             =   1560
      Visible         =   0   'False
      Width           =   1215
   End
   Begin MSComctlLib.TreeView TVW 
      Height          =   4095
      Left            =   0
      TabIndex        =   0
      Top             =   120
      Width           =   6735
      _ExtentX        =   11880
      _ExtentY        =   7223
      _Version        =   393217
      LineStyle       =   1
      Style           =   7
      Checkboxes      =   -1  'True
      Appearance      =   1
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub Form_Load()
    ' 初始化控件的内容
    Call InitTreeData
    TVW.Nodes(1).Selected = True    ' 根节点为当前节点
    TVW.Nodes(1).EnsureVisible      ' 根节点可见
End Sub

' 初始化控件的内容,为TreeView控件增加4层目录
Private Sub InitTreeData()
    ' 首先增加最顶级,即根目录
    TVW.Nodes.Add , , "R", "c:\"
    
    ' 调用递归函数增加后5级目录
    AddDir TVW.Nodes(1).Index, 1, 6
        
    TVW.Nodes(1).Expanded = True
End Sub

' 递归函数,为当前节点及其同层节点增加各自的子节点,到nLastLevel级节点结束
Private Sub AddDir(ByVal nCurIndex As Integer, ByVal nLevel As Integer, ByVal nLastLevel As Integer)
    Dim nodIndex As Integer
    
    ' 已经到达结束层
    If nLevel >= nLastLevel Then
        Exit Sub
    End If
    
    ' 获得指定节点的同层第一个节点索引
    nodIndex = TVW.Nodes(nCurIndex).FirstSibling.Index
    
    ' 为该层节点逐个增加子节点
    Do While nodIndex <> TVW.Nodes(nCurIndex).LastSibling.Index
    ' 当前节点不是该层的最后一个节点时
        
        ' 为指定节点增加子节点
        AddNodeForOne nodIndex
        If TVW.Nodes(nodIndex).Children > 0 Then
        ' 如果有子节点,则为所有子节点加子节点
            AddDir TVW.Nodes(nodIndex).Child.Index, nLevel + 1, nLastLevel
        End If
        
        ' 指向下一个同层节点
        nodIndex = TVW.Nodes(nodIndex).Next.Index
    Loop
    
    ' 为同层最末节点增加子节点
    AddNodeForOne nodIndex
    If TVW.Nodes(nodIndex).Children > 0 Then
    ' 如果有子节点,则为所有子节点加子节点
        AddDir TVW.Nodes(nodIndex).Child.Index, nLevel + 1, nLastLevel
    End If

End Sub

' 为指定节点增加子节点
Private Sub AddNodeForOne(ByVal nodIndex As Integer)
    Dim nodItem As Node
    Dim i As Integer

    Set nodItem = TVW.Nodes(nodIndex)    ' 当前节点
    Dir1.Path = TVW.Nodes(nodIndex).Text  ' 当前目录
    i = 0
    Do While i < Dir1.ListCount          ' 增加子目录做子节点,以父节点的Key+i做新节点的Key
        TVW.Nodes.Add nodItem.Key, tvwChild, nodItem.Key & "-" & Format(i), Dir1.List(i)
        i = i + 1
    Loop
    
    nodItem.Expanded = True
End Sub

' 选取节点
Private Sub TVW_NodeCheck(ByVal Node As MSComctlLib.Node)
     gCheckChildrenBySelf TVW, Node.Index, Node.Checked
     gCheckParentBySibling TVW, Node.Index
End Sub

' 根据自身选取情况,确定全选或取消其下所有子项
Private Sub gCheckChildrenBySelf(TVW As TreeView, ByVal curIndex As Integer, ByVal bCh As Integer)
    Dim n As Integer

    If TVW.Nodes(curIndex).Children <= 0 Then
        Exit Sub
    Else
        n = TVW.Nodes(curIndex).Child.Index
        Do While n <> TVW.Nodes(curIndex).Child.LastSibling.Index
            TVW.Nodes(n).Checked = bCh
            gCheckChildrenBySelf TVW, n, bCh
            n = TVW.Nodes(n).Next.Index
        Loop
        TVW.Nodes(n).Checked = bCh
        gCheckChildrenBySelf TVW, n, bCh
    End If
End Sub

' 根据同层、同父节点的选取情况,确定是否选取其父(乃至更上层),直至根节点
Private Sub gCheckParentBySibling(TVW As TreeView, ByVal curIndex As Integer)
    Dim n As Integer
    Dim bHaveChecked As Boolean

    If TVW.Nodes(curIndex).FirstSibling.Index = 1 Then
        Exit Sub
    Else
        bHaveChecked = False
        n = TVW.Nodes(curIndex).FirstSibling.Index
        Do While n <> TVW.Nodes(curIndex).LastSibling.Index
            If TVW.Nodes(n).Checked = True Then
                bHaveChecked = True
                Exit Do
            End If
            n = TVW.Nodes(n).Next.Index
        Loop
        If TVW.Nodes(n).Checked = True Then
            bHaveChecked = True
        End If
        
        If bHaveChecked = True Then
            TVW.Nodes(curIndex).Parent.Checked = vbChecked
        Else
            TVW.Nodes(curIndex).Parent.Checked = vbUnchecked
        End If
        
        gCheckParentBySibling TVW, TVW.Nodes(curIndex).Parent.Index
    End If
End Sub


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -