Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

TreeView Control in VB.Net

sanjoygrsanjoygr Posts: 14Member
How to select say node 5 in a node tree at the start of the application.

For eg MytreeView has 6 nodes under a main node.
City
---Kolkata
---Chennai
---Delhi
---Mumbai
---Ahmedabad
---Asansol
---Hyderabad

Now by default my selected node should be say 3rd element here Delhi.
Where City is the root node and all other its child node.


Comments

  • kainsworthkainsworth Posts: 200Member
    The trick here is to enumerate through the nodes collection until reaching the index you want and then select that one.
    Assuming that your treeview in the example has only one main node ("City") containing the various child nodes, then this code will select the third child node ("Delhi" in your example)
    [code]
    Dim CityNode As TreeNode = TreeView1.Nodes.Item(0)
    Dim node As TreeNode
    For Each node In CityNode.Nodes
    If node.Index = 2 Then TreeView1.SelectedNode = node
    Next
    [/code]
    Notes:
    1. If there is more than one node which has the same level as "City" (ie if City has sibling nodes) then you will need to expand the above logic so that all of those parent nodes are first enumerated until the City node is found. Then the above code may be used to select the third node.
    2. Watch out for a ListView Gotcha - the ListView's HideSelection property being set to True. This is the default. Make sure it is set to False, otherwise the node will be selected correctly but the user may receive no visual feedback if the Listview does not have focus.

    If you wish to select an item based on its name, not its index, then just change the code to:
    [code]
    ' Assumes that City is the first Node in Treeview
    Dim CityNode As TreeNode = TreeView1.Nodes.Item(0)
    Dim node As TreeNode
    For Each node In CityNode.Nodes
    If node.Text = "Delhi" Then TreeView1.SelectedNode = node
    Next
    [/code]
    Hope this helps

    Ged


  • sanjoygrsanjoygr Posts: 14Member
    It was to select the 1st node.

    TreeView1.SelectNode=TreeView1.Nodes(0).FirstNode


    This is will make the first node selected as default.



  • kainsworthkainsworth Posts: 200Member
    : It was to select the 1st node.
    :
    : TreeView1.SelectNode=TreeView1.Nodes(0).FirstNode
    :
    :
    : This is will make the first node selected as default.
    :
    :
    :
    :
    Sorry, did I misunderstand your question? You've confused me totally now :-) I thought you wanted to select the third or the fifth item, according to your original question(?).
    First and last nodes are fairly straightforward and - almost - as you have just posted.
    Umm, actually it won't quite work as you have it. I think you must have meant :
    [code]
    TreeView1.SelectedNode = TreeView1.Nodes(0).FirstNode
    [/code]
    ie "SelectedNode" not "SelectNode".


Sign In or Register to comment.