代码之家  ›  专栏  ›  技术社区  ›  Developer

是否可以通过编程方式选择其中一个子节点

  •  0
  • Developer  · 技术社区  · 14 年前

    我想从我的树视图中选择一个子节点,通过程序选择。我的树视图在运行时如下

          Root
           |->A.txt(I would like to select this node after doing some iteration in my application)
             |->Child(Even if i select this node and do some operations i would like to select the above one only like that if n number of child nodes exists i would like to select the node that was with .txt extension)
    

    我已经编写了以下代码,它可以很好地工作,但在某一点上,我无法做到这一点,任何人都能帮助我

    这是我的密码

         if (tvwACH.Nodes.Count != 0)
                {
                   // tvwACH.ExpandAll();
                    TreeNode tn;
                    tn = tvwACH.Nodes[0];
                    tvwACH.ExpandAll();
                    if  (tn.Nodes.Count != 0)
                    {
                        tn = tn.Nodes[0];
                    }
                    if (tn.Tag.ToString() == "3")
                    {
                        if (tvwACH.SelectedNode.Parent != null)
                        {
                            tn.Parent.Expand();
                            tvwACH.SelectedNode = tn;
                        }
                    }
                }
    

    我最后的树视图如下

                Root
                  |->Some.txt
                    |->Child
                      |->Sub Child
                         |->Child (for subchild) // After this i will not have any nodes so my code works up to Sub Child but if i added some thing after clicking Child after subchild  i am unable to select the node i meeded as it has no nodes can any one help me out please
    
    1 回复  |  直到 14 年前
        1
  •  0
  •   Developer    14 年前

            TreeNode TvNode = tvwACH.SelectedNode.Parent;
    
                while (TvNode != null)
                {
                    if (TvNode.Text.EndsWith(".txt", true, System.Globalization.CultureInfo.InvariantCulture))
                    {
                        tvwACH.SelectedNode = TvNode;
                        TvNode = null;
                    }
                    else
                        TvNode = TvNode.Parent;
                }