How to select the child node in TreeView in c# windows form

可紊 提交于 2020-05-15 05:07:37

问题


I have one tree view in my windows form. I use the following function to select the node in that treeView.

private void FindAndSelect(TreeNodeCollection collection, object toSelect)
    {
        //problem in this line becouse while converting the toSelect into IstructuredEntity is showing null.

        var entityToSelect = toSelect as Decoupling::IStructureEntity;

        if (entityToSelect == null) //just select the Structure root
        {
            _treeView.SelectedNode = _treeView.Nodes[0];
            return;
        }
        foreach (TreeNode tn in collection)
        {
            var treeNodeEntity = tn.Tag as IStructureEntity;
            if (treeNodeEntity != null && treeNodeEntity.Id == entityToSelect.Id)
            {

                _treeView.SelectedNode = tn;

            }

            FindAndSelect(tn.Nodes, toSelect);
        }
    }

But the above function is only able to select the parent node in treeView and I want to select and highlight the child. Can anyone please guide me as to what I need to change for this to work?


回答1:


TreeView.Nodes will only give you the Parent Nodes. You might have to implement ParentNode.ChildNode to get the childnodes in your tree. Click here for more info

foreach (TreeNode tn in treeView1.Nodes)
{
   // get parent node here
   foreach (TreeNode child in tn.Nodes)
   {
     //get child node here
   }
}


来源:https://stackoverflow.com/questions/29937895/how-to-select-the-child-node-in-treeview-in-c-sharp-windows-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!