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:
From when parent creates the child form you attach to its formClosed event where you check what child did close and make the appropriate treenode change. Example parent code:
VB.NET:
Sub createsomeform()
  Dim f As New Form2
  f.Name = "someform"
  AddHandler f.FormClosed, AddressOf childForms_FormClosed
  f.Show()
End Sub
 
Private Sub childForms_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
  Dim f As Form = DirectCast(sender, Form)
  MsgBox(f.Name & " closed")
End Sub
 
Thanks for the reply John...I understand that... What I don't understand is what I need to do to change the Treeview node from an open folder state back to a closed non selected state.
 
Collapse it:
VB.NET:
[SIZE=2]
TreeView1.Nodes([/SIZE][SIZE=2][COLOR=#a31515]"Node1"[/COLOR][/SIZE][SIZE=2]).Collapse()
[/SIZE]
 
Collapse it:
VB.NET:
[SIZE=2]TreeView1.Nodes([/SIZE][SIZE=2][COLOR=#a31515]"Node1"[/COLOR][/SIZE][SIZE=2]).Collapse()[/SIZE]

I don't want to collapse the whole tree John just the one child node under the main node.

example.

example1.jpg



I just want to close the folder for Davis, Adam T once I've left focus on the Node.

How do I track the nodes? They're not Numerically numbered nodes.
and adding a for next statement to run through the entire list in the tree every time you switch leave the focus would cause sluggish program response.

See once the person opens 2 records for example... I have two children forms open on my screen.

If I click one of the child forms the last node clicked is still open. Even if the child form I'm viewing isn't the last node I clicked in the treeview control.
I want the treeview to track which child form I have focus on and open the folder for it in the treeview as I switch Child MDI forms.

There must be a way to sync the two.
 
Any node can be Expand or Collapse. Just find the node you want and do it to it.
 
Any node can be Expand or Collapse. Just find the node you want and do it to it.

lol *sigh* I was hoping for a simpler answer.

Any ideas on how I can track the Node?

See When I populate the Treeview control There is no Numerical order to the nodes (that I can see). So afterselecting a node I don't know a fast way of finding the node I clicked outside of the Treeview.

For instance if I want to collapse the node after closing a child form I have to scan through all of my Treeview nodes to compair the contents of the node to the name of my childform to find the correct node to collapse it.

You can see how this can become a problem. The larger the database becomes the more nodes to search through the longer it takes to find the correct node to collapse.

Any Ideas?
 
hey

i dont have time to test this properly, but it could work.

in the declarations of your form place this...
VB.NET:
private n as TreeNode

in the after_select event of your treeview, place this..

VB.NET:
if yourTreeView.Selectednodes.count>0 then
n = yourTreeView.SelectedNode
end if

in the lostfocus event of your treeview place this...
VB.NET:
yourTreeView.Nodes(n.name.tostring).collapse

the code in the after select event checks for any child nodes of the selected treenode, if it finds some then it is interpreted as a 'root' node (depending on how deep you go in the treeview) ,and it sets a new selectednode, if it doesnt have any nodes it does nothing (as its impossible to collapse a node that has no child nodes)


hope that works mate :)
have a good one
 
Last edited:
Anti-Rich,
ok the selectednodes.count statement doesn't work as selectednodes.count is not a member of System.Windows.Forms.Treeview. I did rename it to try selectednode.count but it's value is 0 and so never hits the code.

I removed the if statement and set n everytime just to test the code.

When I reached the lostfocus event the .nodes(n.name.tostring).collapse doesn't work as it is empty.

I did however try n.text and I can print the contents and see it's the correct node being stored into the n variable.

now I just need to figure out how to trigger the collapse for the n variable at the lost focus event.
 
Treeview.Nodes collection can be accessed by index or string key, which is Name property of Treenode. It also got the Find method that support searching childs nodes-nodes-nodes.. else you have to use standard recursive methods like this (post3). When assigning nodes give them some usable identifier. When starting the child form from node click you also utilize this identifier and pass it to the form "in some form", for example form name or form Tag property or form custom property for this very purpose. When the form is closed you can read out this same indentifier from the FormClosed event handler and this enables you to find that same treenode where it all started and collapse it again.
 
sorry, i think that statement should have been

VB.NET:
 if yourTreeView.SelectedNode.Nodes.count > 0 
 
...


sorry about that *sheepish grin* it should work now
 
Last edited:
Anti-Rich,
*sigh* no go mate. NullReferenceException in the LostFocus section, Object reference not set to an instance of an object.
 
hm.. thats a tad strange because its working for me. lol !?!?!

ok, this is the code in my form (i tested it out)

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] n [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TreeNode[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TreeView1_AfterSelect([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.TreeViewEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TreeView1.AfterSelect[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] TreeView1.SelectedNode.Nodes.Count > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]n = TreeView1.SelectedNode[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TreeView1_LostFocus([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TreeView1.LostFocus[/SIZE]
[SIZE=2]TreeView1.Nodes(n.Name.ToString).Collapse()[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]


note that you actually need to double click or select the node, otherwise it wont collapse (because when you click the + on the side of the node, vb doesnt recognise it as 'selected' in the sense that vb does)

try that mate, good luck! :)

regards
adam
 
Last edited:
VB.NET:
   Public Sub tvTreeView_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvTreeView.AfterSelect

        Dim indx As Integer
        Dim child As New frmBrowser()

        If e.Node.Tag = Nothing Then Exit Sub
        CID = e.Node.Tag
        CIDText = e.Node.Text

        For indx = 0 To nWindow - 1
            If CID = Me.MdiChildren(indx).Tag Then
                Me.MdiChildren(indx).Focus()
                Exit Sub
            End If
        Next
        If tvTreeView.SelectedNode.Nodes.Count > 0 Then
            n = tvTreeView.SelectedNode
        End If


        child.MdiParent = Me
        Call FillInfo()
        nWindow = nWindow + 1
        child.Text = CIDText
        child.Tag = CID
        child.Show()

    End Sub

VB.NET:
   Private Sub tvTreeView_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles tvTreeView.LostFocus
        tvTreeView.Nodes(n.Name.ToString).Collapse()

    End Sub

This is what I have and I get the error.
 
Back
Top