我想在TREEVIEW中根据节点的字符,如何查找到这个节点并定位到该节点上?

解决方案 »

  1.   

    //set create treenode object
    TreeNode selectnode,chilenode;private void FindSelectNode(string txtnode)
    {
      foreach(selectnode in trvlist.nodes)
      {
            foreach(chilenode in selectnode.nodes)
            {
                chilenode.text=txtnode;
                chilenode.selectnodes();//选择节点
            }
      }}
      

  2.   

    每个节点都可以设置ID,然后根据ID获取具体的节点
      

  3.   

    我用的方法是用递归函数,
    private void button1_Click(object sender, System.EventArgs e)
    {
        TreeNode node = FindNode(this.treeView1.Nodes[0],"ChildText");    if(node == null)
        {
            MessageBox.Show("I couldn't find!");
        }
        else
        {
            MessageBox.Show("Yes,I find!");
        }
    }private TreeNode FindNode(TreeNode FatherNode , String findText)
    {
        if(FatherNode == null) 
             return null ;    if(FatherNode.Text.Equals(findText))
            return FatherNode;
        else
        {
            foreach(TreeNode childNode in FatherNode.Nodes)
            {
                TreeNode tmpNode = FindNode(childNode,findText);            if(tmpNode != null)
                    return tmpNode ;
            }
        }    return null;
    }
      

  4.   

    首先谢谢大家!
    TO: zhangxiaopin(zxp) 我测试了你的代码,不能实现呢,chilenode.selectnodes();//选择节点也没有找到这个方法TO: zglxh3(noproblem),我也在VS2003中测试了你的代码,还是不能实现.
    我等....
      

  5.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    TreeNode tnRes=null;
    for(int i=0;i<treeView1.Nodes.Count;i++)
    {
    tnRes=CompareNodes(treeView1.Nodes[i]); if(tnRes!=null)
    {
    tnRes.EnsureVisible();
    treeView1.Select();
    treeView1.SelectedNode=tnRes;
    break;
    }
    }
    }
    private TreeNode CompareNodes(TreeNode tnCurr)
    {
    TreeNode tnRes=null;
    for(int i=0;i<tnCurr.Nodes.Count;i++)
    {
    if(tnCurr.Nodes[i].Text.Trim()=="节点文本")
    {
    tnRes=tnCurr.Nodes[i];
    return tnRes;
    }
    else
    {
    tnRes= CompareNodes(tnCurr.Nodes[i]);
    }
    }
    return tnRes;
    }
      

  6.   

    TO lijipan(多空
    你的代码我用过了,还是不行啊??
      

  7.   

    if(tnCurr.Nodes[i].Text.Trim()=="节点文本")
    这句要你自己修改.我自己试过是可以的.
      

  8.   

    多空你好!
    我用了你的代码,在装载了树后,节点还是不能展开,光标不能定位到相应的节点上,我的代码如下:
     private TreeNode CompareNodes(TreeNode tnCurr)
            {
                TreeNode tnRes=null;
                for(int i=0;i<tnCurr.Nodes.Count;i++)
                {
                    if(tnCurr.Nodes[i].Text.Trim()=="税法练习")
                    {
                        tnRes=tnCurr.Nodes[i];
                        return tnRes;
                    }
                    else
                    {
                        tnRes= CompareNodes(tnCurr.Nodes[i]);
                    }
                }
                return tnRes;
            }        private void button1_Click(object sender, System.EventArgs e)
            {
                TreeNode tnRes=null;
                for(int i=0;i<treeView1.Nodes.Count;i++)
                {
                    tnRes=CompareNodes(treeView1.Nodes[i]);                if(tnRes!=null)
                    {
                        tnRes.EnsureVisible();
                        treeView1.Select();
                        treeView1.SelectedNode=tnRes;
                        break;
                    }
                }
            
            }