Changing Treeview's selectedNode in Afterselect event.

G

Guest

Hi,
I've a problem with setting the selectednode in a treeview. When a user
clicks a node on the tree, I save the data for the previous node, and store
the newnode in a variable in the Mouse up event. Then I reuild the tree, and
set the SelectedNode to the newnode. I can see that the selected node is
correct till the AfterSelect event - correct in BeforeSelect event. But in
the AFterSelect the SelectedNode changes to a random node. I tried to set the
correct node in the AfterSelect, but it shows this random node. If I set the
selected node to nothing, it becomes nothing, but I cannot set it to this
newnode selected by the user.Can someone suggest what to do here.

Thanx in Advance,
Debi

***************************************
Private Sub tvSchedule_AfterSelect(ByVal sender As Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs) Handles tvSchedule.AfterSelect
Try
Dim Mydate As Date
Dim CurrRow As DataRow
Dim drTreeTag As DataRow
If bTreeMove Or bReload Then Exit Sub
If Not tvSchedule Is Nothing And Not CurrTreeNode Is Nothing Then
If bBugFix And (tvSchedule.SelectedNode.Text <>
CurrTreeNode.Text) Then
tvSchedule.SelectedNode = Nothing
tvSchedule.SelectedNode = CurrTreeNode
bBugFix = False
End If
End If
CurrTreeNode = tvSchedule.SelectedNode
----------------------------------------------------------------------------------------------
Private Sub tvSchedule_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles tvSchedule.MouseUp
Try
Dim pt As Point
pt = New Point(e.X, e.Y)
CurrTreeNode = CType(sender, TreeView).GetNodeAt(pt)
If Not bReload Then
tvSchedule.SelectedNode = CurrTreeNode
End If
If bReload Then
ReloadTree()
bBugFix = True
End If
--------------------------------------------------------------------------------------------
Private Sub tvSchedule_AfterExpand(ByVal sender As Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs) Handles tvSchedule.AfterExpand
If Not bTreeLoad Then tvSchedule.SelectedNode = e.Node()
End Sub
--------------------------------------------------------------------------------------------
Private Sub tvSchedule_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tvSchedule.Click
Try

bTreeClick = True
savedata()
bTreeClick = False
Catch ex As Exception
ShowMessage("Exception Tree Click", AppName, 1)
Finally

End Try

End Sub
 
C

Claes Bergefall

You're saving the selected node in a variable and then clearing
and repopulating the tree, correct?

Do you insert it again? I.e. do you do
tvSchedule.Nodes.Add(CurrTreeNode)?

If you don't then CurrTreeNode doesn't exist in
the tree anymore (since you cleared the tree), so
you won't find it there.

The SelectedNode property selects the EXACT instance
of the node that you're providing. If that instance isn't
inserted into the tree nothing will happen. It doesn' matter
if there is a node with the same text etc. If it's not the
exact same instance nothing will happen

/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top