Solved

Could not Set TreeView.SelectedNode

Posted on 2007-12-04
5
1,680 Views
Last Modified: 2012-05-05
Hi
I am creating windows application using VB.NET 2005
and i have a TreeView on my form.
after select a node and do some operation
then i am repapulating TreeView
then i want to Highlight Node which user was clicked or selected.

i am saving user last selected node in a Node
here i smy code
  Private MySeletedNode As TreeNode
Private Sub TreeView_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView.AfterSelect
 MySeletedNode = TreeView.SelectedNode
End Sub

Private sub LoadData()
 TreeView.Nodes.Clear()
--Adding Nodes here
'here setting Slected node is LastSelectedNode
 TreeView.SelectedNode = MySeletedNode
End Sub

but TreeView.SelectedNode  is showing Nothing
is any wrong in my code?
0
Comment
Question by:RamanaChoudary
  • 2
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20408473
you cannot do that, you cannot "save" the selection, clear and repopulate.
But you can do this:
give each tree node some kind of key or ID, a number, a name. Store it in the TreeViewNode.Tag member.
Clear your control and repopulate.
WHILE repopulating, compare the ID of the nodes with the saved ID, if you find a match, set to SelectedNode to that node.
0
 
LVL 4

Author Comment

by:RamanaChoudary
ID: 20412200
ya thats what i am doing there..
but no luck..
  ''here Expanding the perticular node which was user selected.
            If Not MySeletedNode Is Nothing Then
                TreeView.SelectedNode = MySeletedNode
                For Each RootNode As TreeNode In TreeView.Nodes
                    For Each ChildNode As TreeNode In RootNode.Nodes
                        If ChildNode.Text = MySeletedNode.Parent.Text Then
                            TreeView.SelectedNode = MySeletedNode
                            TreeView.Focus()
                        End If
                    Next
                Next
            End If
0
 
LVL 4

Accepted Solution

by:
RamanaChoudary earned 0 total points
ID: 20413111
Never Mind i got the solution...
i need to check each Main node Chaild Node and Grand Child node
here is the code
If Not MySelectedNode Is Nothing Then
                For Each RootNode As TreeNode In TreeView.Nodes
                    If RootNode.Text = MySelectedNode.Text Then TreeView.SelectedNode = RootNode
                    For Each ChildNode As TreeNode In RootNode.Nodes
                        If ChildNode.Text = MySelectedNode.Text Then TreeView.SelectedNode = ChildNode
                        For Each GrandChildNode As TreeNode In ChildNode.Nodes
                            If GrandChildNode.Text = MySelectedNode.Text Then TreeView.SelectedNode = GrandChildNode
                        Next
                    Next
                Next
            End If

now working good
0
 
LVL 1

Expert Comment

by:Vee_Mod
ID: 20465364
Closed, 100 points refunded.
Vee_Mod
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now