Santosh Kokatnur
Santosh Kokatnur

Reputation: 364

How to display TreeView items to ListView Items in C#

I have the tree view in my windows application and in the tree view with the checkboxes and I have the Some "parent nodes" and some "child nodes" and i want to CHECK AND UNCHECK the parent and child nodes at a time on clicking the "Check All" and "Uncheck All" button... How should i do this?

Now, If i click particular Parent node as well as subfolders, It should be display it in listview!

enter code here

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


        private void Form1_Load(object sender, EventArgs e)
        {
            trvMeals.CheckBoxes = true;
            foreach (TreeNode node in trvMeals.Nodes)
            {
                ExpandNode(node);
            }


            trvMeals.SelectedNode = trvMeals.Nodes[0];
            foreach (TreeNode node in trvMeals.Nodes)
            {
                node.Checked = true;
            }
        }
        private void ExpandNode(TreeNode node)
        {
            node.EnsureVisible();
            foreach (TreeNode child in node.Nodes)
            {
                ExpandNode(child);
            }
        }


        private void trvMeals_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;
            bool is_checked = node.Checked;
            foreach (TreeNode child in node.Nodes)
            {
                child.Checked = is_checked;
            }
            trvMeals.SelectedNode = node;
        }

        private void trvMeals_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in trvMeals.Nodes)
            {
                node.Checked = true;
                CheckChildren(node, true);
            }
            //foreach (TreeNode node in trvMeals.Nodes)
            //{
            //    node.Checked = true;

            //}
        }

        private void button2_Click(object sender, EventArgs e)
        {

            foreach (TreeNode node in trvMeals.Nodes)
            {
                node.Checked = false;

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
           listView1.Text = (listView1 + "\n") + trvMeals.SelectedNode.Text;
            //foreach (TreeNode node in trvMeals.Nodes)
            //{
            //    RcTxt.Text = RcTxt.Text + trvMeals.SelectedNode.Text;
            //}
        }

        private void CheckChildren(TreeNode rootNode, bool isChecked)
        {
            foreach (TreeNode node in rootNode.Nodes)
            {
                CheckChildren(node, isChecked);
                node.Checked = isChecked;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listView1.Clear();
        }
    }
}

Upvotes: 3

Views: 5113

Answers (1)

Ian
Ian

Reputation: 30823

One way to do this is that you can create event handler for AfterSelect event of the TreeView.

Something like this

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
    //grab the listView object and sender as TreeView. 
    //grab whatever you need from the TreeView
    //Put whatever you need in the listView
}

To do the checking and unchecking, creates another event handler for TreeView event AfterCheck.

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) {
    TreeView view = sender as TreeView;         
    //Access the treeview nodes, check/uncheck them
}

Edit:

Expanded code:

private void updateChildrenNodes(TreeNode node, bool isChecked) //recursive call
{
    node.Checked = isChecked;
    if (node.Nodes.Count > 0) //has children, do recursive call            
        foreach (TreeNode childNode in node.Nodes)
            updateChildrenNodes(childNode, isChecked);            
}

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
    TreeView view = sender as TreeView;
    TreeNode node = view.SelectedNode;
    bool isChecked = node.Checked;
    updateChildrenNodes(node, isChecked);
}

List<TreeNode> checkedNodes = new List<TreeNode>();
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    TreeView treeView = sender as TreeView;
    listView1.Clear(); //reset all the nodes
    nodes.Clear(); //clears the list

    //grab whatever you need from the TreeView, check if the TreeNode is checked
    //do the same trick by recursive call to put the checked nodes to checkedNodes list

    foreach (TreeNode checkedNode in checkedNodes)
    {
        //do something, use this info to put in listView1
    }            
}

I have expanded the code such that you have better idea. Hope it may help.

Note: I haven't thoroughly checked the code since it may take sometime, but I believe this should be sufficient for you to get the necessary tools to use as well as the basic idea.

Upvotes: 2

Related Questions