-
Sep 2nd, 2004, 09:32 AM
#1
Thread Starter
Hyperactive Member
set selected node in vb.Net
I have treeview node, mynode, and want to set it as treeview's selected node. I use
tvw.selectedNode = mynode,
but it does not work, the selectedNode is still Nothing after the setting. Help please.
-
Sep 2nd, 2004, 11:00 AM
#2
Addicted Member
be sure mynode is not NOTHING.
-
Sep 2nd, 2004, 11:16 AM
#3
Hyperactive Member
This code creates a new node, adds it to the nodes collection of the tree, sets the selected node property and then uses the selectednode.text property to write a trace line, it works great.
VB Code:
Private Sub frmChild_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myNode As New TreeNode("Root")
Me.TreeView1.Nodes.Add(myNode)
Me.TreeView1.SelectedNode = myNode
Trace.WriteLine(Me.TreeView1.SelectedNode.Text)
End Sub
Whadayamean it doesn't work....
It works fine on my machine!
-
Sep 2nd, 2004, 12:15 PM
#4
Thread Starter
Hyperactive Member
Thanks all of you two.
myNode is not nothing.
to CyberHawke:
myNode is from my treeview, so it is there all the time, I can not add it to the tree again. All I want to do is to highlight it and expand its children.
-
Sep 2nd, 2004, 12:19 PM
#5
Hyperactive Member
This should work:
VB Code:
Private Sub frmChild_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myNode As TreeNode = Me.TreeView1.Nodes(0)
Me.TreeView1.SelectedNode = myNode
myNode.Expand()
Trace.WriteLine(Me.TreeView1.SelectedNode.Text)
End Sub
Whadayamean it doesn't work....
It works fine on my machine!
-
Sep 2nd, 2004, 12:28 PM
#6
Thread Starter
Hyperactive Member
that is what I have tried. no lucky so far.
This is what I am doing:
I click a node and save that node to a node variable, mynode. Then I go to a button to do something else, and the tree get reload with the same set of data. I want to set its selected node to mynode, but I get error "System.nullReferenceException occurred" on myNode.
-
Sep 2nd, 2004, 12:31 PM
#7
Hyperactive Member
That is because the reference that you had to the node is no longer valid, you wiped it out when you reloaded your tree.
Whadayamean it doesn't work....
It works fine on my machine!
-
Sep 2nd, 2004, 12:33 PM
#8
Thread Starter
Hyperactive Member
would you tell me how to make that works?
-
Sep 2nd, 2004, 12:41 PM
#9
Hyperactive Member
Well, you would have to know something about the node that you were working with, what it's text is would be best if the text for all nodes is unique. Then you are left with basically walking the tree node by node (including child nodes) until you find it again.
The statement above assumes that something about your tree has changed so that you do not know the ordinal position of the node.
Whadayamean it doesn't work....
It works fine on my machine!
-
Sep 2nd, 2004, 12:49 PM
#10
Thread Starter
Hyperactive Member
oh, i see. I need loop through all the nodes to find that one and set that node to selected.
-
Sep 2nd, 2004, 12:49 PM
#11
Hyperactive Member
Whadayamean it doesn't work....
It works fine on my machine!
-
Sep 2nd, 2004, 12:51 PM
#12
Thread Starter
Hyperactive Member
thank you for pushing me onto the right track.
Have a good day!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|