TreeView question

Hi

I have a project which works reasonably as expected - apart from one aspect.

I have a TreeView which is populated with folders and files in a typical Windows Explorer sort of way.

However, in the  TreeView_NodeMouseClick event handler, where it populates a Folder node with the files in that folder, I cannot find a way to stop it from adding a fresh batch of files to the node each time the node is clicked. eg when I first click on the node, it opens with the first batch of files in it, when I click again it collapses the node after adding a new batch of the same files to the node, and of corse, when I next open the node it has 3 lots of the files there.

I have tried to find a way to 'Exit Sub' if the node has already been populated, but can't find anything that doesn't give me errors.

For example, if there are no files in the node at all, I will get  'Object reference not set to an instance of an object.' on a code line which does work OK if there are files present, or similar.

How do I extract something from the

ByVal sender As ObjectByVal e As TreeNodeMouseClickEventArgs

parameters of the event handler that would allow me to skip the following code if that node has already been populated

Should I be populating the nodes with the files in the TreeView_NodeMouseClick event handler in the first place I am just doing this because my first attempt took ages to open due to fully populating the tree at start up.




Answer this question

TreeView question

  • John Talbott

    Hi ReneeC

    OK, I get the drift :)

    This Treeview chapter is not a short one :)

    From your hint, I still didn't really get any farther, *but*, looking at those suggestions prompted me to do the following just before adding each filename node to the subnode being populated:

    For i = 0 To newSelected.Nodes.Count - 1

    If newSelected.Nodes(i).Text = aNode.Text Then flag = True

    Next

    If Not flag Then newSelected.Nodes.Add(aNode)

    which does the job. I still feel that this solution isn't as clean as I would have liked though. I would really have liked a

    newSelected.Nodes.AddifNotThereAlready(aNode)

    but of course we can't have everything :)



  • Harinadh

    Isn't the question...

    "Does this node have this data "



  • voidlogic

    In the NodeMouseClick event handler, I was using

    Dim newSelected As TreeNode = e.Node

    which I think I got from the help system somewhere. If I'm not mistaken, that is equivalent to

    Dim foo As TreeNode = SelectedNode

    I used newselected to be the node that I wanted the file names added to. I do see what you meant about the TreeView being a large subject though :)

    My problems seemed to be that the each tree node was already populated with Folders, to which I was adding Files. So that any test for a node being empty was of no use since I really only needed to know if the node contained file names.

    I am just about ready to post my next TreeView question. I hope you can manage to point me in the right direction for it too :)



  • GoodNews

     

    Hey Leshay...

    This isn't one question... it's a career

    Notice that there are other events... Mousedown... mouse up... and

    BeforeSelect and AfterSelect

    BeforeSelect gives you a chance to test certain conditions... after select gives you a chnce to act on those tests.

    The treeview control obscurely offers you a rich set of options.

    Check out e.items in the event argument and also

    the Selecteditem property and the selecteditems collection are very useful.

    It doesn't look like I've said much here, but I've said a ton.

    Please reflect. The treeview is not one instrument.... it's a concert.

     



  • Thomas_K

    Here's a little trick that it takes people to learn.... watch closely and if you get this

    this is one of the secrets of OOP.

    If a node is selected....

    dim foo as treenode = selectedNode

    is interesting because foo acts like a pointer to the slected node.

    However selectedNode has all the properties of a treenode so you can do the following:

    If treeview1.selectednode IsNot nothing then

    whatever

    Endif

    ....or....

    If treeview1.selectednode is nothing then exit sub

    I think this is the cleanway



  • TreeView question