Solved

Could not Set TreeView.SelectedNode

Posted on 2007-12-04
5
1,684 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS.net 2010 11 48
VB.net Move a class from Solution Items to a project 2 33
vb.net cycle through results of a sql query and capture variables 3 18
System.net.Sockets Error 5 27
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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