主题:TreeView控件的使用实例(问题是代码有误,运行不出!!!)
Private Function Exist(node As String) As Boolean
For i = 1 To TreeView1.Nodes.Count
If TreeView1.SelectedItem.Children > 0 Then
If node = TreeView1.Nodes(i).Text Then Exist = True
End If
Next i
End Function
Private Sub Form_Load()
Dim nodex As node
Cbo1.AddItem "北方大学"
Cbo1.AddItem "中文系"
Cbo1.AddItem "物理系"
Set nodex = TreeView1.Nodes.Add(, , "北方大学", "北方大学")
Set nodex = TreeView1.Nodes.Add("北方大学", tvwChild, "中文系", "中文系")
Set nodex = TreeView1.Nodes.Add("北方大学", tvwChild, "物理系", "物理系")
Set nodex = TreeView1.Nodes.Add("中文系", tvwChild, "中文一班", "中文一班")
Set nodex = TreeView1.Nodes.Add("中文系", tvwChild, "中文二班", "中文二班")
Set nodex = TreeView1.Nodes.Add("物理系", tvwChild, "物理一班", "物理一班")
Set nodex = TreeView1.Nodes.Add("物理系", tvwChild, "物理二班", "物理二班")
Call CmdExtr_Click
End Sub
Private Sub CmdAdd_Click() '添加新结点
Dim child As String '存放子结点名
Dim father As String '存放父结点名
If Cbo1.Text = " " Then Exit Sub
father = Cbo1.Text
If Exist(Txt2.Text) = True Then
MsgBox "您输入的班级已存在,请重新输入!", vbOKOnly, "提示"
Else
child = Txt2.Text
Set nodex = TreeView1.Nodes.Add(father, tvwChild, child, child)
Cbol.AddItem child
End If
Txt2.Text = " "
End Sub
Private Sub CmdExtr_Click()
For i = 1 To TreeView1.Nodes.Count
TreeView1.Nodes(i).Expanded = True '将所有结点展开
Next i
End Sub
Private Sub CmdPac_Click()
For i = 1 To TreeView1.Nodes.Count
TreeView1.Nodes(i).Expanded = False '将所有结点收起
Next i
End Sub
Private Sub CmdRem_Click()
If TreeView1.SelectedItem.Index <> 1 Then
TreeView1.Nodes.Remove TreeView1.SelectedItem.Index '删除选定的结点
End If
End Sub
Private Sub Cmdquit_Click()
Unload Me
End Sub