vb中的treeview控件如何使用
在treeview控件中已添加完父节点和子节点我想完成的操作是选择父节点或子节点时组合框中会显示我选择的节点的标题而且在文本框中会显示我选择节点相对应的值...
在treeview控件中已添加完父节点和子节点 我想完成的操作是 选择父节点或子节点时 组合框中会显示我选择的节点的标题 而且在文本框中会显示我选择节点相对应的值
展开
3个回答
展开全部
使用控件需要知道,要想获得树状结构的每个层次中的子文件夹,必须使用递归过程来完成,下面所示代码所执行的工作是:
先把一个已经路径的文件夹添加到树状结构中,然后再遍历它的子文件夹:
'添加文件夹
Public Sub AddDirectory(ByVal strFatherPath As String, ByVal strPath As String, ByVal nodeFather As TreeNode)
Dim i As Integer
Dim node As New TreeNode
'先添加本目录,从文件夹路径分析出文件夹名称
node.Text = Strings.Replace(strPath, strFatherPath & "\", "", , 1)
'为单个节点指定节点未被选中时显示的图标
node.ImageIndex = 1
'为单个节点指定节点被选中时显示的图标
node.SelectedImageIndex = 2
nodeFather.Nodes.Add(node)
Application.DoEvents()
Try
Dim str() As String = iDir.GetDirectories(strPath)
'遍历该目录的子文件夹
For i = 0 To str.GetUpperBound(0)
AddDirectory(strPath, str(i), node)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
node = Nothing
End Sub
然后在按钮的Click事件中添加代码,用于添加树状结构的顶层以及调用遍历子文件的递归过程,详细代码如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim node As New TreeNode
'先把C盘添加到树中
TreeView1.Nodes.Clear()
node.ImageIndex = 0
node.Text = "本地磁盘 C:"
node.SelectedImageIndex = -1
TreeView1.Nodes.Add(node)
Dim i As Integer
'获取C:盘根目录下的文件夹
Dim str() As String = iDir.GetDirectories("C:\")
For i = 0 To str.GetUpperBound(0)
'调用遍历过程
AddDirectory("C:", str(i), node)
Next
node = Nothing
End Sub
先把一个已经路径的文件夹添加到树状结构中,然后再遍历它的子文件夹:
'添加文件夹
Public Sub AddDirectory(ByVal strFatherPath As String, ByVal strPath As String, ByVal nodeFather As TreeNode)
Dim i As Integer
Dim node As New TreeNode
'先添加本目录,从文件夹路径分析出文件夹名称
node.Text = Strings.Replace(strPath, strFatherPath & "\", "", , 1)
'为单个节点指定节点未被选中时显示的图标
node.ImageIndex = 1
'为单个节点指定节点被选中时显示的图标
node.SelectedImageIndex = 2
nodeFather.Nodes.Add(node)
Application.DoEvents()
Try
Dim str() As String = iDir.GetDirectories(strPath)
'遍历该目录的子文件夹
For i = 0 To str.GetUpperBound(0)
AddDirectory(strPath, str(i), node)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
node = Nothing
End Sub
然后在按钮的Click事件中添加代码,用于添加树状结构的顶层以及调用遍历子文件的递归过程,详细代码如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim node As New TreeNode
'先把C盘添加到树中
TreeView1.Nodes.Clear()
node.ImageIndex = 0
node.Text = "本地磁盘 C:"
node.SelectedImageIndex = -1
TreeView1.Nodes.Add(node)
Dim i As Integer
'获取C:盘根目录下的文件夹
Dim str() As String = iDir.GetDirectories("C:\")
For i = 0 To str.GetUpperBound(0)
'调用遍历过程
AddDirectory("C:", str(i), node)
Next
node = Nothing
End Sub
展开全部
Private Sub Form_Load()
TreeView1.Nodes.Add , , "aa", "aa"
TreeView1.Nodes.Add "aa", tvwChild, "bb", "bb"
TreeView1.Nodes.Add "bb", tvwChild, "cc", "cc"
TreeView1.Nodes.Add "bb", tvwChild, "dd", "dd"
TreeView1.Nodes.Add "aa", tvwChild, "ee", "ee"
TreeView1.Nodes.Add "ee", tvwChild, "ff", "ff"
TreeView1.Nodes.Add , tvwChild, "gg", "gg"
Dim i As Integer
For i = 1 To TreeView1.Nodes.Count
TreeView1.Nodes.Item(i).Expanded = True
TreeView1.Nodes.Item(i).Tag = String(100, i & "")
Combo1.AddItem TreeView1.Nodes.Item(i).Text
Next
End Sub
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Combo1.ListIndex = Node.Index - 1
Text1.Text = Node.Tag
End Sub
TreeView1.Nodes.Add , , "aa", "aa"
TreeView1.Nodes.Add "aa", tvwChild, "bb", "bb"
TreeView1.Nodes.Add "bb", tvwChild, "cc", "cc"
TreeView1.Nodes.Add "bb", tvwChild, "dd", "dd"
TreeView1.Nodes.Add "aa", tvwChild, "ee", "ee"
TreeView1.Nodes.Add "ee", tvwChild, "ff", "ff"
TreeView1.Nodes.Add , tvwChild, "gg", "gg"
Dim i As Integer
For i = 1 To TreeView1.Nodes.Count
TreeView1.Nodes.Item(i).Expanded = True
TreeView1.Nodes.Item(i).Tag = String(100, i & "")
Combo1.AddItem TreeView1.Nodes.Item(i).Text
Next
End Sub
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Combo1.ListIndex = Node.Index - 1
Text1.Text = Node.Tag
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Form_Load()
TreeView1.Nodes.Add , , "aa", "aa"
TreeView1.Nodes.Add "aa", tvwChild, "bb", "bb"
TreeView1.Nodes.Add "bb", tvwChild, "cc", "cc"
TreeView1.Nodes.Add "bb", tvwChild, "dd", "dd"
TreeView1.Nodes.Add "aa", tvwChild, "ee", "ee"
TreeView1.Nodes.Add "ee", tvwChild, "ff", "ff"
TreeView1.Nodes.Add , tvwChild, "gg", "gg"
Dim i As Integer
For i = 1 To TreeView1.Nodes.Count
TreeView1.Nodes.Item(i).Expanded = True
TreeView1.Nodes.Item(i).Tag = String(100, i & "")
Combo1.AddItem TreeView1.Nodes.Item(i).Text
Next
End Sub
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Combo1.ListIndex = Node.Index - 1
Text1.Text = Node.Tag
End Sub
TreeView1.Nodes.Add , , "aa", "aa"
TreeView1.Nodes.Add "aa", tvwChild, "bb", "bb"
TreeView1.Nodes.Add "bb", tvwChild, "cc", "cc"
TreeView1.Nodes.Add "bb", tvwChild, "dd", "dd"
TreeView1.Nodes.Add "aa", tvwChild, "ee", "ee"
TreeView1.Nodes.Add "ee", tvwChild, "ff", "ff"
TreeView1.Nodes.Add , tvwChild, "gg", "gg"
Dim i As Integer
For i = 1 To TreeView1.Nodes.Count
TreeView1.Nodes.Item(i).Expanded = True
TreeView1.Nodes.Item(i).Tag = String(100, i & "")
Combo1.AddItem TreeView1.Nodes.Item(i).Text
Next
End Sub
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
Combo1.ListIndex = Node.Index - 1
Text1.Text = Node.Tag
End Sub
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询