ARTICLE

How to add a node to TreeView Control at runtime in VB.NET

Posted by Shalini Juneja Articles | ASP.NET using VB.NET March 21, 2011
In this article you will learn what is TreeView Control,its property and how you can add a node to this control at runtime
Download Files:
 
Reader Level:

A TreeView control of Visual basic is used to Display hierarical data in a tree format.this control is Helpful to represent the organization structure, file system or any other system in which you need hierarchical representation.this control is similar to windows explorer which shows a TreeView in left side to show all folder on the hard disk.In TreeView Control You can add nodes and sub nodes.this control provides you the option to add the nodes at design time as well as at runtime.you generally add the nodes at runtime.you can expand and collpase these nodes by clicking on them.the default Event of the TreeView Control is AfterSelect Event.

Properties of TreeView Control:-

  • TreeView:-you can get the parent TreeView.
     
  • Nodes:-you can get the collection of nodes in the current node.
     
  • FirstNode:-you can get the first child tree node.
     
  • LastNode:-you can get the last child Node.
     
  • NextNode:-you can get the next sibling node.


    How to add a node in TreeView control at runtime:-
     
  • Open a new Project
     
  • Drag the TreeView Control on form like below

    TreeView1.gif
     
  • Write the code behind form load

        Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles
            MyBase.Load
            With TreeView1
                .BeginUpdate()
                .Nodes.Add("Node0")
                .Nodes(0).Nodes.Add("Node1")
                .Nodes(0).Nodes.Add("Node2")
                .Nodes.Add("Node3")
                .Nodes(1).Nodes.Add("Node4")
                .Nodes(1).Nodes.Add("Node5")
                .Nodes.Add("Node6")
                .Nodes(2).Nodes.Add("Node7")
                .EndUpdate()
            End With
        End
    Sub
     

  • Select the AfterSelect Event of TreeView Control and add below code on this Event

       Private Sub TreeView1_AfterSelect(ByVal sender As Object,ByVal e As
            System.Windows.Forms.TreeViewEventArgs)Handles TreeView1.AfterSelect
            MessageBox.Show(TreeView1.SelectedNode.FullPath())
        End
    Sub

  • Now execute the above code.node will be added

    Output:-


    TreeView3.gifTreeView2.gif

    TreeView5.gifTreeView4.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor