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

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.

Edited 5 Years Ago by nick.crane: n/a

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);
               
            }
            
        }
This question has already been answered. Start a new discussion instead.