Changing treeNode with child form event

Troy

Well-known member
Joined
Feb 7, 2005
Messages
153
Programming Experience
10+
When I click on a tree node it changes my graphic to an open folder and opens a child form as it should.

When I close the child form the node still shows an open folder and I have to click a different node then go back to the previous node to reselect it and open a child form.

Is there a way to open the child form but when I close the child have the treeview node close the folder or basically reset it?


hmmm I could have sworn I named this just Question? This actually isn't about changing just the node picture I really need to reset the node to an unclicked state after I close the child form pertaining to the node.
 
Last edited:
try removing this line from your cod e
VB.NET:
If e.Node.Tag = Nothing Then Exit Sub

it is advisable to avoid using exit sub as much as possible, as it can do some really weird things. the second you call exit sub, nothing else is executed. it simply breaks out, possibly leaving stuff out that needs to execute.

actually!!! now that i think of it thats why its chucking the nullreference exception because the program doesnt get down the if statement i supplied for you (therefore not assigning n), as its getting to a point in your program and exiting the sub (it might not be that specific line, but i know that the 'exit sub' statement will have something to do with it), so try and remove all references to exit sub in your program and make your program handle it a different way.

ok, first things first move the

VB.NET:
 If tvTreeView.SelectedNode.Nodes.Count > 0 Then
            n = tvTreeView.SelectedNode
        End If

up to the top. and then watch what happens.

also, why are you using e instead of the selectednode property of your treeview?

so yeah, try what i said, good luck! :D
 
I would also bet much that the null reference is the 'n' variable, which you of course can check if is Nothing before trying to access its members.
But why do you "expand" the node on click, just to "collapse" it the very same millisecond when it loose focus?
 
I would also bet much that the null reference is the 'n' variable, which you of course can check if is Nothing before trying to access its members.
But why do you "expand" the node on click, just to "collapse" it the very same millisecond when it loose focus?

The whole purpose behind this is I want to have the treeview nodes highlight and open when I select the child forms already opened in the window to the right. Right now I can select a different child form to the right and the Treeview Just looses focus but does not close the open folder.

I want for example, If I click the child form to the right and it is for Adam T Davis, I want the treeview node for Adam T. Davis to also be open and selected
 
Back to the original question:
When I click on a tree node it changes my graphic to an open folder and opens a child form as it should.
...
Is there a way to open the child form but when I close the child have the treeview node close the folder or basically reset it?
I think this was properly answered in post 10. I can't see how treeview focus has anything to do with this.
 
Back to the original question:

I think this was properly answered in post 10. I can't see how treeview focus has anything to do with this.


hehe ok let me try again.... When you select a name in the treeview list it changes the closed folder, by the name in the treeview node, to an open folder and opens an MDI child form in the right side of the screen.

When I select a second client from the treeview the same thing occures as planned, the previous node graphic changes from an open folder to a closed folder, this works fine.

Now here's where everything changes.

If I select a different already opened child form on the right side of the screen the highlighted child node doesn't change. If I have 2 open children say, one for Adam T. Davis and another for Aziz Michael, and The last one I had opened was Aziz Michael. The Node in the treeview with his name would be highlighted still and focus would be on the newly created child form with his name in the title.

I then decide to click the child form for Adam T. Davis, not closing any forms, just changing focus from one child form to another. The Treeview selection does not change. The node for Aziz Michael would still be open even though the child form I'm now viewing is for Adam T. Davis.

I need a way for when I select a child form, I can have the Treeview node change to reflect which child form I'm now working with. When I click on Adam T. Davis I want the Treeview node to have the open folder for Adam T. Davis not Aziz Michael.

The other problem with this being the way it currently is, is when I change focus to a different child form from say Adam T. Davis to Aziz Michael's form, If I then close the Child for Adam T. Davis the Node for Adam T. Davis is still in the selected state and I cannot reopen a child for that unless I click a different node in my Treeview first, then click the node for Adam T. Davis again to reopen a child form.

It should be as easy as clicking a node to open the child form. When you close the child it deselects the treeview node and basically everything is resetted so I can reclick that node and reopen the form if I want.

Agaon this functionality works when just selecting from the treeview control but if change focus with any open children forms and close any of those the treeview control doesn't know this and isn't updated to reflect the changes.

If you'll notice in my screenshot the child form for Carl Broom has focus but the treeview node for Adam T. Davis is the one with the open folder. It should be the node for Carl Broom that is open.

I need a way to manipulate the treeview nodes when I change focus on the child forms.

:(
 
ok, now I see your "focus" part of the problem, but it is not a treeview focus problem, it regards active mdi child. You can set one image index for a treenodes initial state and one for its selected state.

I have now attached a sample project where all the requested functionality is implemented. The "End Class linecount" is 55 and includes much code comments so this should be easy to understand.

:Edit: Nearly "opposite" of what I said in post 2 & 10 I found it better to reset any treeview selection when mdi child form closed, and find node and select it when child form activated. The way of identifying and hooking child form events to select/deselect treenodes still applies and is implemented in the attached project.
 

Attachments

  • vbnet20-TreeviewMdiSync.zip
    21.9 KB · Views: 160
Last edited:
ok, now I see your "focus" part of the problem, but it is not a treeview focus problem, it regards active mdi child. You can set one image index for a treenodes initial state and one for its selected state.

I have now attached a sample project where all the requested functionality is implemented. The "End Class linecount" is 55 and includes much code comments so this should be easy to understand.

:Edit: Nearly "opposite" of what I said in post 2 & 10 I found it better to reset any treeview selection when mdi child form closed, and find node and select it when child form activated. The way of identifying and hooking child form events to select/deselect treenodes still applies and is implemented in the attached project.


You are once again my hero John! It still took some time for me to understand your code, but thats because we have different programming styles. I'm old school trying to catchup on the new commands and you happen to already know them and your programming style reflects that. I had to adapt how you populated your TreeNodes with my way of populating them to use my DataTables. Once I understood that it all fell into place. Everything is working wonderfully now thanks to tracking the nodes with the key.

Now I have to undo all the other clutter I added to try to make it work. I wish I knew all the commands you know. You're code was much more condensed than mine and you knew you could sort the Treeview control with a command where I had my code sort then populate my treeview nodes already sorted. Your's saved alot of code.

Thanks once again. *bows to the Master Guru* :D
 
Last edited:
Glad it sorted out!

It takes some time to get familiar with the full framework and learning to utilize it. I'm not by far through even browsing all the classes and members yet, let alone reading the extended info about each member and trying it all out in code..
 
Back
Top