[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Could not Set TreeView.SelectedNode

Posted on 2007-12-04
5
Medium Priority
?
1,719 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
4 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month19 days, 20 hours left to enroll

872 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