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

📄 form1.frm

📁 这里有很多很实用的VB编程案例,方便大家学习VB.
💻 FRM
字号:
VERSION 5.00
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.3#0"; "COMCTL32.OCX"
Begin VB.Form Form1 
   Caption         =   "TreeView控件属性"
   ClientHeight    =   6195
   ClientLeft      =   2475
   ClientTop       =   1710
   ClientWidth     =   6510
   LinkTopic       =   "Form1"
   PaletteMode     =   1  'UseZOrder
   ScaleHeight     =   6195
   ScaleWidth      =   6510
   Begin VB.CommandButton cmdClear 
      Caption         =   "Clear Treeview"
      Height          =   375
      Left            =   4200
      TabIndex        =   8
      Top             =   3240
      Width           =   2055
   End
   Begin VB.CommandButton cmdFirst 
      Caption         =   "Add First Sibling"
      Height          =   375
      Left            =   4200
      TabIndex        =   7
      Top             =   360
      Width           =   2055
   End
   Begin VB.CommandButton cmdLast 
      Caption         =   "Add Last Sibling"
      Height          =   375
      Left            =   4200
      TabIndex        =   6
      Top             =   840
      Width           =   2055
   End
   Begin VB.CommandButton cmdNext 
      Caption         =   "Add Next Sibling"
      Height          =   375
      Left            =   4200
      TabIndex        =   5
      Top             =   1320
      Width           =   2055
   End
   Begin VB.CommandButton cmdPrevious 
      Caption         =   "Add Previous Sibling"
      Height          =   375
      Left            =   4200
      TabIndex        =   4
      Top             =   1800
      Width           =   2055
   End
   Begin VB.CommandButton cmdChild 
      Caption         =   "Add Child"
      Height          =   375
      Left            =   4200
      TabIndex        =   3
      Top             =   2280
      Width           =   2055
   End
   Begin VB.CommandButton cmdRemove 
      Caption         =   "Remove Node"
      Height          =   375
      Left            =   4200
      TabIndex        =   2
      Top             =   2760
      Width           =   2055
   End
   Begin VB.CommandButton cmdClose 
      Caption         =   "Close"
      Height          =   375
      Left            =   4200
      TabIndex        =   1
      Top             =   3720
      Width           =   2055
   End
   Begin ComctlLib.TreeView TreeView1 
      Height          =   5535
      Left            =   240
      TabIndex        =   0
      Top             =   360
      Width           =   3855
      _ExtentX        =   6800
      _ExtentY        =   9763
      _Version        =   327682
      Style           =   7
      ImageList       =   "ImageList1"
      BorderStyle     =   1
      Appearance      =   1
   End
   Begin ComctlLib.ImageList ImageList1 
      Left            =   4200
      Top             =   4200
      _ExtentX        =   1005
      _ExtentY        =   1005
      BackColor       =   -2147483643
      ImageWidth      =   16
      ImageHeight     =   16
      MaskColor       =   12632256
      _Version        =   327682
      BeginProperty Images {0713E8C2-850A-101B-AFC0-4210102A8DA7} 
         NumListImages   =   2
         BeginProperty ListImage1 {0713E8C3-850A-101B-AFC0-4210102A8DA7} 
            Picture         =   "Form1.frx":0000
            Key             =   ""
         EndProperty
         BeginProperty ListImage2 {0713E8C3-850A-101B-AFC0-4210102A8DA7} 
            Picture         =   "Form1.frx":031A
            Key             =   ""
         EndProperty
      EndProperty
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Option Explicit
Dim mnIndex As Integer
Dim mbIndrag As Boolean
Dim moDragNode As Object
Private Sub GetFirstParent()
    On Error GoTo myerr
    Dim i As Integer
    Dim nTmp As Integer
    
    For i = 1 To TreeView1.Nodes.Count
        nTmp = TreeView1.Nodes(i).Parent.Index
    Next
    Exit Sub
    
myerr:
    mnIndex = i
    Exit Sub
End Sub

Private Function GetNextKey() As String
    Dim sNewKey As String
    Dim iHold As Integer
    Dim i As Integer
    On Error GoTo myerr
    iHold = Val(TreeView1.Nodes(1).Key)
    For i = 1 To TreeView1.Nodes.Count
        If Val(TreeView1.Nodes(i).Key) > iHold Then
            iHold = Val(TreeView1.Nodes(i).Key)
        End If
    Next
    iHold = iHold + 1
    sNewKey = CStr(iHold) & "_"
    GetNextKey = sNewKey
    Exit Function
myerr:
    GetNextKey = "1_"
    Exit Function
End Function



Private Sub cmdChild_Click()
    Dim oNodex As Node
    Dim skey As String
    Dim iIndex As Integer
        
    On Error GoTo myerr
    iIndex = TreeView1.SelectedItem.Index
    skey = GetNextKey()
    Set oNodex = TreeView1.Nodes.Add(iIndex, tvwChild, skey, "Child " & skey, 1, 2)
    '使子节点可见。
    oNodex.EnsureVisible
    Exit Sub
myerr:
    MsgBox ("You must select a Node to do an Add Child" & vbCrLf _
       & "If the TreeView is empty us Add Last to create the first node")
    Exit Sub
End Sub

Private Sub cmdClear_Click()
    Cls
    TreeView1.Nodes.Clear
End Sub

Private Sub cmdClose_Click()
    Unload Me
End Sub

Private Sub cmdFirst_Click()
    Dim skey As String
    Dim iIndex As Integer
    
    On Error GoTo myerr
    iIndex = TreeView1.SelectedItem.Index
    skey = GetNextKey()
    TreeView1.Nodes.Add iIndex, tvwFirst, skey, "First " & skey, 1, 2
    Exit Sub
myerr:
    MsgBox ("You must select a Node to do an Add First" & vbCrLf _
       & "If the TreeView is empty us Add Last to create the first node")
    Exit Sub
    
End Sub

Private Sub cmdLast_Click()
    Dim skey As String
    skey = GetNextKey()
    On Error GoTo myerr
    TreeView1.Nodes.Add TreeView1.SelectedItem.Index, tvwLast, skey, "Last " & skey, 1, 2
    Exit Sub
myerr:
    TreeView1.Nodes.Add , tvwLast, skey, "Last " & skey, 1, 2
    Exit Sub
End Sub








Private Sub cmdNext_Click()
    Dim skey As String
    Dim iIndex As Integer
    
    On Error GoTo myerr
    iIndex = TreeView1.SelectedItem.Index
    skey = GetNextKey()
    TreeView1.Nodes.Add iIndex, tvwNext, skey, "Next " & skey, 1, 2
    Exit Sub
myerr:
    MsgBox ("You must select a Node to do an Add Next" & vbCrLf _
       & "If the TreeView is empty us Add Last to create the first node")
    Exit Sub
End Sub

Private Sub cmdPrevious_Click()
    Dim skey As String
    Dim iIndex As Integer
    On Error GoTo myerr
    iIndex = TreeView1.SelectedItem.Index
    skey = GetNextKey()
    TreeView1.Nodes.Add iIndex, tvwPrevious, skey, "Previous " & skey, 1, 2
    Exit Sub
myerr:
    MsgBox ("You must select a Node to do an Add Previous" & vbCrLf _
       & "If the TreeView is empty us Add Last to create the first node")
    Exit Sub
End Sub



Private Sub cmdRemove_Click()
    Dim iIndex As Integer
    
    On Error GoTo myerr
    iIndex = TreeView1.SelectedItem.Index
    TreeView1.Nodes.Remove iIndex
    Exit Sub
myerr:
    MsgBox ("You must select a Node to do a Remove" & vbCrLf _
       & "If the TreeView is empty us Add Last to create the first node")
    Exit Sub
End Sub






Private Sub Form_Load()
    Set moDragNode = Nothing
    cmdLast_Click
    cmdLast_Click
    TreeView1.Nodes(1).Selected = True
    cmdChild_Click
End Sub

Private Sub TreeView1_DragDrop(Source As Control, x As Single, y As Single)

If TreeView1.DropHighlight Is Nothing Then
        mbIndrag = False
        Exit Sub
    Else
        On Error GoTo checkerror
        Set moDragNode.Parent = TreeView1.DropHighlight
        Cls
        Print TreeView1.DropHighlight.Text & _
        " is parent of " & moDragNode.Text
        Set TreeView1.DropHighlight = Nothing
        mbIndrag = False
        Set moDragNode = Nothing
        Exit Sub
    End If
 
checkerror:
    
    Const CircularError = 35614
    If Err.Number = CircularError Then
        Dim msg As String
        msg = "A node can't be made a child of its own children."

        If MsgBox(msg, vbExclamation & vbOKCancel) = vbOK Then
            mbIndrag = False
            Set TreeView1.DropHighlight = Nothing
            Exit Sub
        End If
    End If


End Sub

Private Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
    If mbIndrag = True Then
        Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
    End If
End Sub

Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
    If Not TreeView1.DropHighlight Is Nothing Then
        TreeView1.SelectedItem = TreeView1.HitTest(x, y)
        Set moDragNode = TreeView1.SelectedItem
    End If
    Set TreeView1.DropHighlight = Nothing
End Sub

Private Sub TreeView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = vbLeftButton Then
        mbIndrag = True
        TreeView1.DragIcon = TreeView1.SelectedItem.CreateDragImage
        TreeView1.Drag vbBeginDrag ' Drag operation.
    End If

End Sub




⌨️ 快捷键说明

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