Results 1 to 12 of 12

Thread: set selected node in vb.Net

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    471

    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.

  2. #2
    Addicted Member
    Join Date
    Apr 2004
    Location
    Lagos, Nigeria
    Posts
    215
    be sure mynode is not NOTHING.

  3. #3
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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:
    1. Private Sub frmChild_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim myNode As New TreeNode("Root")
    3.         Me.TreeView1.Nodes.Add(myNode)
    4.         Me.TreeView1.SelectedNode = myNode
    5.         Trace.WriteLine(Me.TreeView1.SelectedNode.Text)
    6.     End Sub
    Whadayamean it doesn't work....
    It works fine on my machine!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    471
    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.

  5. #5
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    This should work:
    VB Code:
    1. Private Sub frmChild_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim myNode As TreeNode = Me.TreeView1.Nodes(0)
    3.         Me.TreeView1.SelectedNode = myNode
    4.         myNode.Expand()
    5.         Trace.WriteLine(Me.TreeView1.SelectedNode.Text)
    6.     End Sub
    Whadayamean it doesn't work....
    It works fine on my machine!

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    471
    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.

  7. #7
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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!

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    471
    would you tell me how to make that works?

  9. #9
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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!

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    471
    oh, i see. I need loop through all the nodes to find that one and set that node to selected.

  11. #11
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    correct
    Whadayamean it doesn't work....
    It works fine on my machine!

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    471
    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
  •  



Click Here to Expand Forum to Full Width