Hi,

I have one main form with a panel called pnl. On the panel are a few controls including a Treeview (tv1).

Now, when the user clicks a certain button, the panel pnl (including the treeview) is hidden, and a new form opens, showing the same treeview.
(Basically, you could see it as 'undocking' the treeview from the main form into a seperate form)

Now, I want the treeview in the new seperate form to have the same state (that is, the same nodes opened) as the treeview I have just hidden.


I tried this code but for some reason it is not working:

In the main form, in the 'undock' button click event:
vb.net Code:
  1. If frmSCP.Created = True Then
  2.       frmSCP.Visible = True
  3. Else
  4.       frmSCP.Show()
  5. End If
  6. pnl.Visible = False
(frmSCP is the seperate treeview form)

I check for the creation of frmSCP first because loading the contents of the treeview takes a little time and I don't want it to load it all over again each time the form is loaded. Therefore I simply hide it (Visible = False) and re-show it (Visible = True) again if it was opened before.


In frmSCP I have the following code:
vb.net Code:
  1. Private Sub btnDock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDock.Click
  2.         frmMain.pnl.Visible = True
  3.         frmMain.tv1.CollapseAll()
  4.         frmMain.tv1.SelectedNode = tv1.SelectedNode
  5.         Me.Visible = False
  6.     End Sub
  7.  
  8.     Private Sub frmSCP_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
  9.         tv1.CollapseAll()
  10.         tv1.SelectedNode = frmMain.tv1.SelectedNode
  11.     End Sub
  12.  
  13.     Private Sub frmSCP_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  14.         LoadTreeViewFromXmlFile(My.Resources.IntelXML, tv1)
  15.         tv1.CollapseAll()
  16.         tv1.SelectedNode = frmMain.tv1.SelectedNode
  17.     End Sub

(LoadTreeViewFromXlmFile() is the treeview loading sub)

As you can see, when the Dock button is pressed, the first thing to happen is to re-show the treeview in the main form. Then, all it's nodes are collapsed. Then, it's SelectedNode property is set to the same SelectedNode property of the treeview in the current form, and finally, the current form is hidden.

When the form is activated or loaded, the treeviews nodes are all collapsed and then the correct node should be selected again.

I know that this will not automatically expand the same nodes as the other treeview, it merely selects the same nodes (but, afaik, if you tell it to select a subnode for example, the parent node will automatically expand, no??)

I am still looking for how to expand the same nodes...


But first, the code above is NOT working! It does not select the same node (not even when I have a parent node selected). I can see no reason why it shouldn't be working... Also note that I have made sure that the new treeview's nodes are first selected, before closing (hiding) the old treeview, so that should give me no problems right??


If anyone has any ideas, please let me know! Thanks.