How to Make Read-only Single Node of Tree View in C# (Windows Application)

Introduction

In this blog, I am going to explain that how to make any single node of tree view Read-only in c#. Steps are below:

Steps:

  1. first time, when you click on Tree View in (windows application) then and do something whatever your requirements, then change the color of tree View node
  2. And whenever you’ll click on that particular node (changed node color in first step), then a message will appear “do you want change node selection”

If user clicks ‘Yes button’ then color of node changes to its original means default color, if ‘no’ then selected node and his root node will be in Read only state. Here below I showing an example with code in c#:
 
Make two methods like as:

1. Call this method in any click event when you want make the node readonly TRUE.

       void ReadOnlyNodeTrue ()

        {

            bool Isexist = false;

            foreach (TreeNode t in MMCtv.Nodes)

            {

                foreach (DataGridViewRow dgvrr in dgvTreeView.Rows)

                {

                    if (dgvrr.Cells["grBatchNo"].Value.ToString() == t.Text.Replace("BatchNo:- ", ""))

                    {

                        if (t.BackColor != System.Drawing.Color.Gray)

                        {

                            t.BackColor = System.Drawing.Color.Honeydew;

                            Isexist = true;

                        }

                    }

                }

                if (Isexist)

                {

                    Isexist = false;

                    foreach (TreeNode t1 in t.Nodes)

                    {

                        foreach (DataGridViewRow dgvrr in dgvTreeView.Rows)

                        {

                            if (dgvrr.Cells["grCompMaterial"].Value.ToString() == t1.Text.Replace("MatCode:- ", ""))

                            {

                                if (dgvrr.Cells["grBatchNo"].Value.ToString() == t.Text.Replace("BatchNo:- ", ""))

                                {

                                    if (t1.BackColor != System.Drawing.Color.Gray)

                                    {

                                        t1.BackColor = System.Drawing.Color.Honeydew;

                                        Isexist = true;

                                    }

                                }

                            }

                        }

                        if (Isexist)

                        {

                            foreach (TreeNode t2 in t1.Nodes)

                            {

                                if (t2.BackColor != System.Drawing.Color.Gray)

                                {

                                    t2.BackColor = System.Drawing.Color.Honeydew;

                                    Isexist = true;

                                }

                            }

                        }

                        Isexist = false;

                    }

                } 

            }

            TreeNode TN1 = MMCtv.SelectedNode;

            TN1.BackColor = System.Drawing.Color.Honeydew;

        }
 
2. Call this method in TreeView_NodeMouseClick  when you want make the node readonly FALSE. 
 

        void ReadOnlyNodeFalse ()

        {

            bool Isexist = false;

            foreach (TreeNode t in MMCtv.Nodes)

            {

                foreach (DataGridViewRow dgvrr in dgvTreeView.Rows)

                {

                    if (dgvrr.Cells["grBatchNo"].Value.ToString() == t.Text.Replace("BatchNo:- ", ""))

                    {

                        if (t.BackColor != System.Drawing.Color.Gray)

                        {

                            t.BackColor = System.Drawing.Color.Empty;

                            Isexist = true;

                        }

                    }

                }

                if (Isexist)

                {

                    Isexist = false;

                    foreach (TreeNode t1 in t.Nodes)

                    {

                        foreach (DataGridViewRow dgvrr in dgvTreeView.Rows)

                        {

                            if (dgvrr.Cells["grCompMaterial"].Value.ToString() == t1.Text.Replace("MatCode:- ", ""))

                            {

                                if (dgvrr.Cells["grBatchNo"].Value.ToString() == t.Text.Replace("BatchNo:- ", ""))

                                {

                                    if (t1.BackColor != System.Drawing.Color.Gray)

                                    {

                                        t1.BackColor = System.Drawing.Color.Empty;

                                        Isexist = true;

                                    }

                                }

                            }

                        }

                        if (Isexist)

                        {

                            foreach (TreeNode t2 in t1.Nodes)

                            {

                                if (t2.BackColor != System.Drawing.Color.Gray)

                                {

                                    t2.BackColor = System.Drawing.Color.Empty;

                                    Isexist = true;

                                }

                            }

                        }

                        Isexist = false;

                    }

                }

            }

            TreeNode TN1 = MMCtv.SelectedNode;

            TN1.BackColor = System.Drawing.Color.Empty;

        }