c#windows窗体中如何在TreeView中选择子节点 [英] How to select the child node in TreeView in c# windows form

查看:35
本文介绍了c#windows窗体中如何在TreeView中选择子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Windows 窗体中有一个树视图.我使用以下函数来选择该树视图中的节点.

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);
        }
    }

但是上面的函数只能选择treeView中的父节点,我想选择并突出显示子节点.任何人都可以指导我了解我需要改变什么才能使其工作吗?

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?

推荐答案

TreeView.Nodes 只会给你父节点.您可能必须实现 ParentNode.ChildNode 才能获取树中的子节点.单击此处 了解更多信息

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
   }
}

这篇关于c#windows窗体中如何在TreeView中选择子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆