Click here to Skip to main content
15,868,051 members
Articles / Programming Languages / Visual Basic
Article

The Basic Operations on using the TreeView Control

Rate me:
Please Sign up or sign in to vote.
2.77/5 (9 votes)
30 Sep 20052 min read 81.2K   1.2K   17   6
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.

VB
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.

VB
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.

VB
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


Written By
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? Pin
fwsouthern1-Oct-05 15:57
fwsouthern1-Oct-05 15:57 
AnswerRe: Oh dear, I already have an "article.source" from another article -- what do I do now? Pin
Daniel Turini3-Oct-05 21:39
Daniel Turini3-Oct-05 21:39 
GeneralWhy authors should give their code a "meaningful" name Pin
Anonymous4-Oct-05 5:49
Anonymous4-Oct-05 5:49 
Ok, guys -- the reason I keep pointing out to authors that they should give their code "meaningful" names is TRACKING updates and tracking code names to article names -- I, as well as others, like to save code for future reference. Sure, I "could" rename the code when I save it, but then "I" have to remember what I named it when the author updates his code. I really don't like having to remember what "I" named a particular piece of code in order to track updates nor do I really want to rename everyone's code. I suggest authors to give their code a meaningful name so that we can track not only updates but relate the code name to the article name (I also print quite a few of these articles) -- picking up an article and saying to myself, just "what" did I name that code and where did "I" put it is a royal pain in the ass!

It's time authors get the message -- no code is perfect and all code is subject to revision/enhancement -- be accommodating to your readers so that they can track your article name to your code and have your code name relate easily to your article purpose.

You are not the only author nor are you the only author to publish a particular article on a particular subject -- others will publish similar articles on similar subjects and just naming your code "Article_source", etc., is confusing at best with other similar articles.

Its a small thing but one that your readers will appreciate.
GeneralRe: Why authors should give their code a "meaningful" name Pin
Smitha Nishant5-Oct-05 7:11
protectorSmitha Nishant5-Oct-05 7:11 
GeneralRe: Why authors should give their code a "meaningful" name Pin
John Turcott22-Oct-05 16:29
sussJohn Turcott22-Oct-05 16:29 
GeneralRe: Why authors should give their code a "meaningful" name Pin
Smitha Nishant3-Nov-05 7:02
protectorSmitha Nishant3-Nov-05 7:02 

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

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