1 Replies - 3959 Views - Last Post: 23 September 2014 - 02:11 AM

#1 andrewsw  Icon User is online

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,568
  • Joined: 12-December 12

Compare and Colour Two TreeViews (WinForms)

Posted 14 August 2014 - 01:49 PM

This code compares two TreeViews and colours nodes that are different. This is only intended for two TreeViews that should be identical. That is, it only colours nodes whose text differs and skips (ignores) any nodes that appear on one tree but not the other. It does not make any attempt to reconcile (find and compare) nodes that are in the wrong position.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication12 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            CompareTreeNodes(this.treeView1, this.treeView2);
        }

        void CompareRecursiveTree(TreeNode tn1, TreeNode tn2) {
            if (tn1.Text != tn2.Text) {
                tn1.ForeColor = Color.Red;
                tn2.ForeColor = Color.Red;
            }
            int compare = Math.Min(tn1.Nodes.Count, tn2.Nodes.Count);
            // ignore extra nodes
            for (int i = 0; i < compare; i++) {
                CompareRecursiveTree(tn1.Nodes[i], tn2.Nodes[i]);
            }
        }

        void CompareTreeNodes(TreeView tv1, TreeView tv2) {
            int compare = Math.Min(tv1.Nodes.Count, tv2.Nodes.Count);
            // ignore extra nodes
            for (int i = 0; i < compare; i++) {
                CompareRecursiveTree(tv1.Nodes[i], tv2.Nodes[i]);
            }
        }
    }
}


The following additions would use different colours if, say, the text of one tree's node is shorter, or longer, than that of the other tree's node. It also demonstrates navigating back to the main/root node and painting that; that is, painting the root node of a branch that contains a difference.
    void CompareRecursiveTree(TreeNode tn1, TreeNode tn2) {
        if (tn1.Text != tn2.Text) {
            //tn1.ForeColor = Color.Red;
            //tn2.ForeColor = Color.Red;

            TreeNode tnp = tn2;
            while (tnp.Parent != null) {
                tnp = tnp.Parent;
            }
            tnp.BackColor = Color.Yellow;
        }
        // colour differently on comparing text
        int areThey = String.Compare(tn1.Text, tn2.Text, StringComparison.OrdinalIgnoreCase);
        if (areThey < 0) {
            tn1.ForeColor = Color.Red;
            tn2.ForeColor = Color.Green;
        } else if (areThey > 0) {
            tn1.ForeColor = Color.Green;
            tn2.ForeColor = Color.Red;
        }
        int compare = Math.Min(tn1.Nodes.Count, tn2.Nodes.Count);
        // ignore extra nodes
        for (int i = 0; i < compare; i++) {
            CompareRecursiveTree(tn1.Nodes[i], tn2.Nodes[i]);
        }
    }

Attached Image

This post has been edited by andrewsw: 14 August 2014 - 02:54 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Compare and Colour Two TreeViews (WinForms)

#2 Ald0s  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-September 14

Re: Compare and Colour Two TreeViews (WinForms)

Posted 23 September 2014 - 02:11 AM

Nice snippet, thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1