hi i am using this code to get the checked treeview node name.... it goes inside the loop only if the parent node is selected...

I have to get the node name if any node is checked...
this is the code i am using..

public void CheckedNames(System.Windows.Forms.TreeNodeCollection theNodes)
        {

            if (theNodes != null)
            {
                foreach (System.Windows.Forms.TreeNode aNode in theNodes)
                {
                    if (aNode.Checked)
                    {
                        aResult.Add(aNode.Text);
                    }

                }
            }

        }

i am calling it from the AfterCheck event

public void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            aResult.Clear();
            CheckedNames(treeView1.Nodes);
        }

what i am doing wrong... help me
vince

Recommended Answers

All 3 Replies

In the AfterCheck event the TreeViewEventArgs contains the node that was just (un)checked.
Try this instead.

private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
	if (e.Node.Checked)
	{
		aResult.Add(e.Node);
	}
	else
	{
		if (aResult.Contains(e.Node))
		{
			aResult.Remove(e.Node);
		}
	}
}
}

BTW.
To get your CheckedNames to work you must use recursion to process any child nodes of each branch.

Thanks for ur reply... i used this code and got the results

private void GetNodeRecursive(TreeNode treeNode)
        {

            foreach (TreeNode tn in treeNode.Nodes)
            {
                GetNodeRecursive(tn);
            }
            if (treeNode.Checked == true)
            {
                aResult.Add(treeNode.Text);
               
            }
            
        }

That would do it.
If solved please mark solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.