积极答复者
请教关于TreeView的问题

问题
-
请教关于TreeView的问题。已知当前节点和它的父节点,我的思路是遍历找到父节点再在父节点添加当前节点,
但是在递归N.Nodes.Count的值总是0,从来没有进入里面的循环,请问要怎么修改?谢谢!Private Sub ListSect() Dim Proc As IProcess '添加根节点 TreeView1.BeginUpdate() TreeView1.Nodes.Add("main process") TreeView1.EndUpdate() '添加子节点 '先查找父ProcessName,找到后再在下面增加当前ProcessName For Each Proc In Job.AllProcesses(False) For Each node As TreeNode In TreeView1.Nodes AddNode(node, Proc.ParentProcesses(0).name, Proc.Name) Next Next End Sub Private Sub AddNode(ByVal N As TreeNode, ByVal ParentNodeText As String, ByVal ChildNodeText As String) For Each node As TreeNode In N.Nodes ' N.Nodes.Count总是为0,从来没有进入下面的循环 If node.Text = ParentNodeText Then node.Nodes.Add(ChildNodeText) End If AddNode(node, ParentNodeText, ChildNodeText) Next End Sub
答案
-
Hi leon1526,
根据你提供的代码,你只是在Treeview1中加入一个根节点"main process", 没有其他任何子节点, 但是你的这行代码是查找根节点下面的所有子节点,因为你没有加入任何一个子节点,所以N.Nodes.count=0
For Each node As TreeNode In TreeView1.Nodes AddNode(node, Proc.ParentProcesses(0).name, Proc.Name) Next
你可以参考以下代码在根节点下面加入子节点,再进行测试你的代码。
Private Sub fun() TreeView2.BeginUpdate() TreeView2.Nodes.Add("main process") TreeView2.Nodes(0).Nodes.Add("Child 1") TreeView2.Nodes(0).Nodes.Add("Child 2") TreeView2.Nodes(0).Nodes(1).Nodes.Add("Grandchild") TreeView2.Nodes(0).Nodes(1).Nodes(0).Nodes.Add("Great Grandchild") TreeView2.EndUpdate() 'For Each node As TreeNode In TreeView2.Nodes ' AddNode(node, "parentText", "ChildText") 'Next End Sub
更多关于TreeView的信息, 请参考一下链接:
https://msdn.microsoft.com/zh-cn/library/system.windows.forms.treeview(v=vs.110).aspx
Best Regards,
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 leon1526 2017年5月6日 10:27
-
Hi Leon1526,
我不是完全确定你想要的功能是否是这个样子。
Dim name As String = "节点名" For Each node As TreeNode In TreeView1.Nodes If node.Name = name Then node.Nodes.Add(New TreeNode("新节点")) End If Next
Best Regards,
Cherry Bu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 leon1526 2017年5月10日 12:59
全部回复
-
Hi leon1526,
根据你提供的代码,你只是在Treeview1中加入一个根节点"main process", 没有其他任何子节点, 但是你的这行代码是查找根节点下面的所有子节点,因为你没有加入任何一个子节点,所以N.Nodes.count=0
For Each node As TreeNode In TreeView1.Nodes AddNode(node, Proc.ParentProcesses(0).name, Proc.Name) Next
你可以参考以下代码在根节点下面加入子节点,再进行测试你的代码。
Private Sub fun() TreeView2.BeginUpdate() TreeView2.Nodes.Add("main process") TreeView2.Nodes(0).Nodes.Add("Child 1") TreeView2.Nodes(0).Nodes.Add("Child 2") TreeView2.Nodes(0).Nodes(1).Nodes.Add("Grandchild") TreeView2.Nodes(0).Nodes(1).Nodes(0).Nodes.Add("Great Grandchild") TreeView2.EndUpdate() 'For Each node As TreeNode In TreeView2.Nodes ' AddNode(node, "parentText", "ChildText") 'Next End Sub
更多关于TreeView的信息, 请参考一下链接:
https://msdn.microsoft.com/zh-cn/library/system.windows.forms.treeview(v=vs.110).aspx
Best Regards,
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 leon1526 2017年5月6日 10:27
-
Hi Leon1526,
我不是完全确定你想要的功能是否是这个样子。
Dim name As String = "节点名" For Each node As TreeNode In TreeView1.Nodes If node.Name = name Then node.Nodes.Add(New TreeNode("新节点")) End If Next
Best Regards,
Cherry Bu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 leon1526 2017年5月10日 12:59