Question

This was a very helpful link for sorting TreeView Nodes in AfterLabelEdit event. It works great. The only problem is that it changes SelectedNode and TopNode to the Root. How can I get it to be the Node that was originally selected (now with a new text value) AND with TopNode set as it was before sorting?

Here is what my code looks like now:

Private Sub tvInventory_AfterLabelEdit(sender As Object, e As System.Windows.Forms.NodeLabelEditEventArgs) Handles tvInventory.AfterLabelEdit
        tvInventory.BeginInvoke(New MethodInvoker(AddressOf tvInventory.Sort))
        e.CancelEdit = False
End Sub

I tried adding code to set the tvInventory.SelectedNode and tvInventory.TopNode values inside the AfterLabelEdit event handler but they are just ignored.

Was it helpful?

Solution

The answer was staring me in the face all along! Use the same BeginInvoke method to restore my place outside the AfterLabelEdit event handler! Final code looks like this:

Private Sub tvInventory_AfterLabelEdit(sender As Object, e As System.Windows.Forms.NodeLabelEditEventArgs) Handles tvInventory.AfterLabelEdit
    SaveMyPlace()
    tvInventory.BeginInvoke(New MethodInvoker(AddressOf tvInventory.Sort))
    tvInventory.BeginInvoke(New MethodInvoker(AddressOf RestoreMyPlace))
    e.CancelEdit = False
End Sub

Private Sub SaveMyPlace()
    SavedSelectedNode = tvInventory.SelectedNode
    SavedTopNode = tvInventory.TopNode
End Sub

Private Sub RestoreMyPlace()
    tvInventory.SelectedNode = SavedSelectedNode
    tvInventory.TopNode = SavedTopNode
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top