Click here to Skip to main content
15,886,639 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.3K   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 
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 
I suppose you are talking about my article code. I assimilated it from snippets I found in the web, so I don't think I need to get the *full* credit. Nice to know it was of help Smile | :)



Every problem has a gift for you in its hands.
-- Richard Bach

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.