Removing CHild Nodes from a treeview

I have a treeview structured as such:

root
1st Child
2nd Child

the 2nd Child nodes are added at Runtime, based on information pulled from the SQL query. Thats all great and fine, but when I want to run a new query, I need for the 2nd Child nodes to Be gone.
Here is the code I wrote to remove the 2nd child nodes:
[code]
For Each t As TreeNode In TreeView1.Nodes 'loop through parent nodes
For Each c As TreeNode In t.Nodes 'loop through 1st child nodes
For Each d As TreeNode In c.Nodes 'loop through 2nd child nodes
d.Remove() 'remove child node System.NullRefException

Next
Next
Next
[/code]

as I step through this code it works fine until it gets to one particular 1st child node that has 5 child nodes under it. It runs through 3 of those 2nd child nodes and removes them like it's supposed to, but then it throws a NullReference exception... "Object reference not setto an instance of an object". I dont understand why all of a sudden it loses the object reference that it just had.????


Comments

  • : I have a treeview structured as such:
    :
    : root
    : 1st Child
    : 2nd Child
    :
    : the 2nd Child nodes are added at Runtime, based on information
    : pulled from the SQL query. Thats all great and fine, but when I want
    : to run a new query, I need for the 2nd Child nodes to Be gone.
    : Here is the code I wrote to remove the 2nd child nodes:
    : [code]:
    : For Each t As TreeNode In TreeView1.Nodes 'loop through parent nodes
    : For Each c As TreeNode In t.Nodes 'loop through 1st child nodes
    : For Each d As TreeNode In c.Nodes 'loop through 2nd child nodes
    : d.Remove() 'remove child node System.NullRefException
    :
    : Next
    : Next
    : Next
    : [/code]:
    :
    : as I step through this code it works fine until it gets to one
    : particular 1st child node that has 5 child nodes under it. It runs
    : through 3 of those 2nd child nodes and removes them like it's
    : supposed to, but then it throws a NullReference exception... "Object
    : reference not setto an instance of an object". I dont understand why
    : all of a sudden it loses the object reference that it just had.????
    :

    Depending on where the debugger is pointing at (?), I think the problem is the following.

    Let me just rewrite part of your above code (it probably won't work anymore, but just to explain something).
    [code]
    For Each d In c.Nodes
    c.Nodes.Remove(d)
    [/code]
    So you're having a loop of the amount of elements in c.Nodes, but per iteration you remove an element.
    So you're messing up your own loop.

    Best thing to do is something like:
    [code]
    For i = 1 To c.Nodes.Count
    c.Nodes.Remove(0)
    Next
    [/code]
    0 is guaranteed to be there if there are elements.


    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : Depending on where the debugger is pointing at (?), I think the
    : problem is the following.
    :
    : Let me just rewrite part of your above code (it probably won't work
    : anymore, but just to explain something).
    : [code]:
    : For Each d In c.Nodes
    : c.Nodes.Remove(d)
    : [/code]:
    : So you're having a loop of the amount of elements in c.Nodes, but
    : per iteration you remove an element.
    : So you're messing up your own loop.
    :
    : Best thing to do is something like:
    : [code]:
    : For i = 1 To c.Nodes.Count
    : c.Nodes.Remove(0)
    : Next
    : [/code]:
    : 0 is guaranteed to be there if there are elements.
    :
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    Richard,
    That makes sense now, but for c.nodes.remove(0), cannot be an integer.
    the param is expected to be a treenode. Is there a way around this?

  • : : Depending on where the debugger is pointing at (?), I think the
    : : problem is the following.
    : :
    : : Let me just rewrite part of your above code (it probably won't work
    : : anymore, but just to explain something).
    : : [code]: :
    : : For Each d In c.Nodes
    : : c.Nodes.Remove(d)
    : : [/code]: :
    : : So you're having a loop of the amount of elements in c.Nodes, but
    : : per iteration you remove an element.
    : : So you're messing up your own loop.
    : :
    : : Best thing to do is something like:
    : : [code]: :
    : : For i = 1 To c.Nodes.Count
    : : c.Nodes.Remove(0)
    : : Next
    : : [/code]: :
    : : 0 is guaranteed to be there if there are elements.
    : :
    : :
    : : Best Regards,
    : : Richard
    : :
    : : The way I see it... Well, it's all pretty blurry
    :
    : Richard,
    : That makes sense now, but for c.nodes.remove(0), cannot be an
    : integer.
    : the param is expected to be a treenode. Is there a way around this?
    :
    :
    Perhaps the nodes.clear() method is an option.
    Note: found it using google "vb removeall nodes"
  • Well this may not be the best way, but i just cleared all the nodes and recreated the treeview. Its simple and it works.
  • : Well this may not be the best way, but i just cleared all the nodes
    : and recreated the treeview. Its simple and it works.
    :

    Whatever works ;)

    But just out of curiosity: perhaps there is an AtIndex function to convert Index to Node?

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories