Home > DeveloperSection > Interviews > What is basic concept for tree view

Posted on    September-16-2016 1:02 AM

 C# C# 
Ratings:
 1 Answer(s)
  83  View(s)
Rate this:

Mark Devid
Mark Devid

Total Post:9

Points:45
Posted on    September-16-2016 1:02 AM

Basic Concept for Tree View 

 

TreeNode is the keyword which is used to represent the node for the Treeview.

 

We need to define the name for the TreeNode

 

eg ;- TreeNode Node1

 

How to add 1st element in the node :

 

syntax  tNode = treeView1.Nodes.Add("Country");

 

treeView1 is the name of the Tree view which is present in your form




Now, we can add many node to this  node.

 

eg. treeView1.Nodes[0].Nodes.Add("India");

 

        we can add many node to 2nd node       

eg treeView1.Nodes[0].Nodes[0].Nodes.Add("Delhi");

 

Here is the complete code we can implement in our coding

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace WindowsFormsApplication18

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            TreeNode node1,tNode;

 

            tNode = treeView1.Nodes.Add("Country");

 

            treeView1.Nodes[0].Nodes.Add("India");

            treeView1.Nodes[0].Nodes[0].Nodes.Add("Delhi");

 

            treeView1.Nodes[0].Nodes.Add("Australia");

            treeView1.Nodes[0].Nodes[1].Nodes.Add("Canberra");

            treeView1.Nodes[0].Nodes[1].Nodes.Add("Perth");

 

            treeView1.Nodes[0].Nodes.Add("NewZealand");

            treeView1.Nodes[0].Nodes[2].Nodes.Add("Auckland");

            treeView1.Nodes[0].Nodes[2].Nodes[0].Nodes.Add("North_land");

        }

        private void button1_Click(object sender, EventArgs e)

        {

            MessageBox.Show(treeView1.SelectedNode.FullPath.ToString());

        }

    }

}

 


Modified On Sep-16-2016 01:18:49 AM

Don't want to miss updates? Please click the below button!

Follow MindStick