This was a very helpful link用于在AfterLabelEdit事件中对TreeView节点进行排序。它很棒。唯一的问题是它将SelectedNode和TopNode更改为Root。如何才能将它作为最初选择的节点(现在使用新的文本值)并将TopNode设置为排序前的节点?
以下是我的代码现在的样子:
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
我尝试添加代码来设置AfterLabelEdit事件处理程序中的tvInventory.SelectedNode和tvInventory.TopNode值,但它们只是被忽略。
答案 0 :(得分:0)
答案一直盯着我!使用相同的BeginInvoke方法在AfterLabelEdit事件处理程序之外恢复我的位置!最终代码如下所示:
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