Click here to Skip to main content
Click here to Skip to main content
Go to top

The Basic Operations on using the TreeView Control

, 30 Sep 2005
Rate this:
Please Sign up or sign in to vote.
How to do basic operations (i.e. add, delete nodes) on the TreeView control in your VB.NET applications.

Introduction

In this article, I have tried to explain the basic usages of TreeView control. Because of this, I'm giving a sample code to show the basic operations (such as add parent and child nodes) on TreeView.

This article is just written to be a reference when you need to use the TreeView control in your VB.NET applications.

Implementation of the Sample

Here, you will find the necessary source code of the application the main form of which is shown above. Each event procedure in the whole source code is explained separately.

The code for "Add Parent" button

It is only one line and creates a parent node by taking its name from the textbox.

Private Sub AddParent_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles AddParent.Click
    TView.Nodes.Add(Text1.Text)
End Sub

The code for "Add Child" button

It is only one line and creates a child node by taking its name from the textbox. It creates a child node under the selected parent node. You can create a child node under another child node.

Private Sub AddChild_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles AddChild.Click
    TView.SelectedNode.Nodes.Add(Text1.Text)
End Sub

The code for "Delete" button

It is only one line again and deletes the selected node whether it is a parent or child. Moreover, you can delete any parent node completely whether it has child nodes.

Private Sub DeleteNode_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles DeleteNode.Click
    TView.SelectedNode.Remove()
End Sub

Important Points

  • Do not forget that if any node has a child, the node is being a parent node of the child.
  • Any parent node can be a child node of a one level up node.
  • If you delete any parent node, the parent node will be deleted completely with its child nodes. If you do not want this to be happen, you will need an extra statement to check the condition of the parent node.
  • In this sample, before you add a new child, you should select a node (as a parent) by clicking on it. Otherwise it may give an error. Moreover, it is the same for the delete operation and you should select the node that you want to delete.
  • This sample application is prepared to be a reference while using the TreeView control in your VB.NET applications.

Have a nice day!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Yiltan Bitirim
Instructor / Trainer
Turkey Turkey
Asst. Prof. in Eastern Mediterranean University.

Comments and Discussions

 
QuestionOh dear, I already have an "article.source" from another article -- what do I do now? Pinmemberfwsouthern1-Oct-05 15:57 
AnswerRe: Oh dear, I already have an "article.source" from another article -- what do I do now? PinmemberDaniel Turini3-Oct-05 21:39 
GeneralWhy authors should give their code a "meaningful" name PinsussAnonymous4-Oct-05 5:49 
GeneralRe: Why authors should give their code a "meaningful" name PinstaffSmitha Vijayan5-Oct-05 7:11 
GeneralRe: Why authors should give their code a "meaningful" name PinsussJohn Turcott22-Oct-05 16:29 
GeneralRe: Why authors should give their code a "meaningful" name PinstaffSmitha Vijayan3-Nov-05 7:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 30 Sep 2005
Article Copyright 2005 by Yiltan Bitirim
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid