Solved

Could not Set TreeView.SelectedNode

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

15 Experts available now in Live!

Get 1:1 Help Now