none
Copy TreeView items checked states from treeview to another ?? RRS feed

  • Question

  • Hi

     

    I have the following situation

     

    please I need any kindly help or guide about copying the checked state between 2 treeviews

     

    I want to make the 2 treeviews equally in their check states

     

    How I can do this

     

    Thanks for any help

     

    Mohamed

     

    Tuesday, July 3, 2007 3:56 PM

Answers

  •  

    Hi AboOmar,

     

    We can use for loop to get the all the checked state and set it to the Form2's treeview. Or We can just copy all the node in the Form1's TreeView to Form2's TreeView. See the code sample below.

    Form1

    Code Snippet

    namespace TreeView

    {

        public partial class CloneTreeView : Form

        {

            public CloneTreeView()

            {

                InitializeComponent();

            }

     

            bool check;

            public System.Windows.Forms.TreeView TreeView1

            {

                get

                {

                    return this.treeView1;

                }

            }

     

            private void btnOpenForm_Click(object sender, EventArgs e)

            {

                CloneTreeViewChild f = new CloneTreeViewChild(this);

                f.Show();

            }

     

            private void btnAddNode_Click(object sender, EventArgs e)

            {

                TreeNode node = new TreeNode("added Node");

                node.Checked = check;

                this.treeView1.Nodes.Add(node);

                check = !check;

            }

        }

    } 

     

    Form2:

    Code Snippet

    namespace TreeView

    {

        public partial class CloneTreeViewChild : Form

        {

            Form form;

     

            public CloneTreeViewChild( Form f)

            {

                InitializeComponent();

                form = f;

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                this.treeView1.Nodes.Clear();

                CloneTreeView f = this.form as CloneTreeView;

                foreach (TreeNode node in f.TreeView1.Nodes)

                {

                    this.treeView1.Nodes.Add((TreeNode)node.Clone());

                }

                this.treeView1.ExpandAll();

            }

        }

    }

     

    Hope this helps.

    Regards

    Thursday, July 5, 2007 5:29 AM

All replies

  • You'll need to refine this: are the tree views identical?  Do you need to copy the state each time one node's check state changes?
    Tuesday, July 3, 2007 6:23 PM
    Moderator
  • Hi

    First , thanks for your reply

     

    but the situation is that : I have two Forms (Form1 and Form2) and each one contains a treeview with the same nodes

     

    and I want within a button-click on the Form2 to copy the checked state to the Form1 treeview to be the same as Form2 treeview

     

    So

     

    how I can achieve this

     

    Thanks

     

    Mohamed

     

     

     

     

    Wednesday, July 4, 2007 8:34 AM
  •  

    Hi AboOmar,

     

    We can use for loop to get the all the checked state and set it to the Form2's treeview. Or We can just copy all the node in the Form1's TreeView to Form2's TreeView. See the code sample below.

    Form1

    Code Snippet

    namespace TreeView

    {

        public partial class CloneTreeView : Form

        {

            public CloneTreeView()

            {

                InitializeComponent();

            }

     

            bool check;

            public System.Windows.Forms.TreeView TreeView1

            {

                get

                {

                    return this.treeView1;

                }

            }

     

            private void btnOpenForm_Click(object sender, EventArgs e)

            {

                CloneTreeViewChild f = new CloneTreeViewChild(this);

                f.Show();

            }

     

            private void btnAddNode_Click(object sender, EventArgs e)

            {

                TreeNode node = new TreeNode("added Node");

                node.Checked = check;

                this.treeView1.Nodes.Add(node);

                check = !check;

            }

        }

    } 

     

    Form2:

    Code Snippet

    namespace TreeView

    {

        public partial class CloneTreeViewChild : Form

        {

            Form form;

     

            public CloneTreeViewChild( Form f)

            {

                InitializeComponent();

                form = f;

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                this.treeView1.Nodes.Clear();

                CloneTreeView f = this.form as CloneTreeView;

                foreach (TreeNode node in f.TreeView1.Nodes)

                {

                    this.treeView1.Nodes.Add((TreeNode)node.Clone());

                }

                this.treeView1.ExpandAll();

            }

        }

    }

     

    Hope this helps.

    Regards

    Thursday, July 5, 2007 5:29 AM