Advertisement

Responsive Advertisement

III.6- Using TreeView Control

 III.6- Using TreeView Control

TreeView control in C# is used to display the items in hierarchical form. Each item in TreeView control is called a node. A node can have sub-nodes too; and each sub-node has it's own nodes, and so on. All the nodes in the TreeView control are displayed in an hierarchical form, for better readability and control.

Add TreeView control to the Form

Create a C# Windows Forms Application in Visual Studio IDE. In Form’s design view, add the TreeView control from the Toolbox. Visual Studio automatically, creates a variable for this control and by using this we can use the features of the control.


You can add the nodes to the control in design view also, by selecting Nodes property from the Properties dialog. Through this Article, we are going to discuss the methods used to manage the control.

Adding nodes to the TreeView control

A Treeview control is the collection of nodes; each node is in the same level or in different hierarchical position in the tree structure. These collection of nodes are managed through it’s Nodes property.


Nodes is of TreeNodeCollection type, that represents the collection of tree nodes assigned to the TreeView control.

Adding sub-nodes to the TreeView control

When we add the node to the TreeView control; it returns the tree node that being added to the collection. By using the tree node, we can add sub-nodes to it. As I mentioned above, each tree node is of TreeNode type.


TreeNode type also has Nodes property (of TreeNodeCollection type) to manage the collection of nodes. Hence, we can use the same way (as above), to add the sub-nodes to the TreeNode. Below code adds the sub-node to the tree node; we got the tree node, from above statement; 





Source Code:

public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private string InputBox(string message, string Title)

        {

            string drive = "";

            int x = (SystemInformation.WorkingArea.Width / 2) - 200;

            drive = Microsoft.VisualBasic.Interaction.InputBox(message, Title, "", x, x);

            return drive;

 

        }

 

        private void btnDrive_Click(object sender, EventArgs e)

        {

            string drive = InputBox("Drive Name", "Drive");

            if (drive == "")

            {

                MessageBox.Show("Invalid drive nane.");

                return;

            }

            TreeNode N = new TreeNode();

            N.Tag = "drive";

            N.Text = drive;

            TV.Nodes.Add(N);

        }

 

        private void btnFolder_Click(object sender, EventArgs e)

        {

            string folder = InputBox("Folder Name", "Folder");

            if (folder == "")

            {

                MessageBox.Show("Invalid folder name.");

                return;

            }

            if (TV.Nodes.Count == 0)

            {

                MessageBox.Show("No drive found.");

                return;

            }

 

            if (TV.SelectedNode == null)

            {

                MessageBox.Show("Select node root first.");

                return;

            }

            else if (TV.SelectedNode.Tag.ToString() == "file")

            {

                MessageBox.Show("Cannot add folder");

                return;

            }

            TreeNode N = new TreeNode();

            N.Text = folder;

            N.Tag = "folder";

            TV.SelectedNode.Nodes.Add(N);

        }

 private void btnFile_Click(object sender, EventArgs e)

        {

            string file = InputBox("File Name", "File");

            if (file == "")

            {

                MessageBox.Show("Invalid file name.");

                return;

            }

            if (TV.Nodes.Count == 0)

            {

                MessageBox.Show("No drive or folder found.");

                return;

            }

            if (TV.SelectedNode == null)

            {

 

                MessageBox.Show("Select node drive or folder.");

                return;

            }

            else if (TV.SelectedNode.Tag.ToString() == "file")

            {

                MessageBox.Show("Cannot add file to file");

                return;

 

            }

            TreeNode N = new TreeNode();

            N.Text = file;

            N.Tag = "file";

            TV.SelectedNode.Nodes.Add(N);

        }

 

        private void btnDelete_Click(object sender, EventArgs e)

        {

            if (TV.SelectedNode == null)

            {

                MessageBox.Show("Select node to delete");

                return;

            }

            DialogResult mess;

            mess = MessageBox.Show("Do you want to delete?", "Delete", MessageBoxButtons.YesNo);

            if (mess == DialogResult.No)

                return;

            TV.Nodes.Remove(TV.SelectedNode);

        }

 

        private void btnSort_Click(object sender, EventArgs e)

        {

            TV.Sort();

        }

 

        private void btnClear_Click(object sender, EventArgs e)

        {

                txtText.Text = TV.SelectedNode.Text;

                txtType.Text = TV.SelectedNode.Tag.ToString();

        }

 

        






















Post a Comment

0 Comments