![]() Then we manipulate the mainList so that we get all values from firstList that aren't in secondList. NewNode = GetLastAddedChildNode(node, level) The code for GetLastAddedChildNode is: Private Function GetLastAddedChildNode(ByVal parentNode As TreeNode,įor Each node As TreeNode In parentNode.Nodes Next we set the U13 as the root node: root = GetLastAddedChildNode(TreeView1.Nodes(0), indexOfLastNode) This is done quite simply: Dim indexOfLastNode As Integer = mainList.Count - 1 Next, since you want to branch out from U13 we need to get the index of this. Note the use of index now instead of parent.Level. There has also been a slight change to the AddMoreChildren method: Private Sub AddMoreChildren(ByVal parent As TreeNode)ĭim child As New TreeNode(mainList(index).ToString()) I set index to 1 here because I already have the root node and want to start at A3 when I go into AddMoreChildren. Now that we have these let's go ahead and add them to the TreeView: index = 1 MainList = firstList.Intersect(secondList).ToList() This is used to get a list where values are found in both firstList and secondList leaving us with S, A3 and U13: Dim root As TreeNode I also have to manipulate mainList quite a bit so I'll try to explain in bits.įirst let's use Intersect. This is purely to keep track of the current index in mainList instead of using If parent.Level. In this I have to bring in another variable called index. ![]() This requires a little more code and isn't as neat but it does do the job. This is what the OP is after based on the update: MainList = firstList.Concat(secondList).Distinct().ToList() If parent.Level < mainList.Count - 1 Thenĭim child As New TreeNode(mainList(parent.Level + 1)) Note that I have now changed my Lists to be of type String for this example and no longer need to use. If you have two lists then what I would look at doing is bringing them together to create a distinct List using Concat and Distinct. ![]() The beauty of this is that the code will continue to call AddMoreChildren until all items in the List have been added. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Loadĭim root As New TreeNode(())įrom the code above you will achieve the same result as seen shown in the screenshot. Private Sub AddMoreChildren(ByVal parent As TreeNode)ĭim child As New TreeNode(list(parent.Level + 1).ToString()) What I would look at instead is recursively adding child Nodes to the TreeView like such: Public Class Form1 This of course is a real pain and isn't scalable. TreeView1.Nodes(0).Nodes.Add(list(1).ToString()) This is what it would look like manually: (list(0).ToString()) If I have got this right, you are trying to add Nodes so that the TreeView looks like this: ![]()
0 Comments
Leave a Reply. |