.net C# Treeview的查找node文字项

来源:互联网 发布:erp软件是什么软件 编辑:程序博客网 时间:2024/06/05 14:04

 

//查找可见项

  private TreeNode GetNode(TreeView tv, TreeNode StartNode, String str)
        
{
            
//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.10
            TreeNode Node;
            str 
= str.ToUpper();
            
if (StartNode == null)
            
{
                Node 
= tv.Nodes[0];
            }

            
else
            
{
                Node 
= StartNode.NextVisibleNode;
            }

            String Text;
            
int p;
            
while (Node != null)
            
{
                Text 
= Node.Text.ToUpper();
                p 
= Text.IndexOf(str);
                
if (p > -1)
                
{
                    
return Node;
                }

                Node 
= Node.NextVisibleNode;
            }

            
return null;
        }

 //查找所有项

//Accurately是否为精确查找

private TreeNode GetNode(TreeView tv, TreeNode StartNode, string str, bool accurately)

        {

            //by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.10

            TreeNode Node;

            if (str == null) return null;

            str = str.ToUpper();

            if (StartNode == null)

            {

                Node = tv.Nodes[0];

            }

            else

            {

                Node = StartNode.NextVisibleNode;

            }

            String Text;

            int p;

           TreeNode ParentNode = Node;

            while (Node != null)

            {

                if (accurately)

                {

                    if (str == Node.Text)

                        return Node;

                }

                else

                {

                    Text = Node.Text.ToUpper();

 

                    p = Text.IndexOf(str);

                    if (p > -1)

                    {

                        return Node;

                    }

                }

               

                if (Node.GetNodeCount(true) > 0) //有子               

                {

                    ParentNode = Node;

                    Node = Node.Nodes[0];

                }

                else

                {

                    Node = Node.NextNode;

                    while ((Node == null) && (ParentNode != null))

                    {

                        Node = ParentNode.NextNode;

                        ParentNode = ParentNode.Parent;

                    }

 

                }

             

            }

            return null;

        }

 

原创粉丝点击