循环查询TreeView控件内容

private void btn_Query_Click(object sender, EventArgs e)
{
    this.AddTreeNodeToList();
    string sQueryKey = this.txt_QueryKey.Text.Trim();
    if (!string.IsNullOrEmpty(sQueryKey) && (this.LstTreeNode.Any()))
    {
        if (this.NodeIndex != 0)
        {
            this.NodeIndex++;
        }
        while (this.NodeIndex < this.LstTreeNode.Count)
        {
            TreeNode trNode = this.LstTreeNode[this.NodeIndex];
            if ((trNode != null) && trNode.Text.Contains(sQueryKey))
            {
                this.QueryTreeView.SelectedNode = trNode;
                break;
            }
            this.NodeIndex++;
        }
        if (this.NodeIndex >= this.LstTreeNode.Count)
        {
            this.NodeIndex = 0;
        }
    }
}

其中this.LstTreeNode为整个树控件所有的结点,添加结点的方法如下:

private void AddTreeNodeToList()
{
    if (this.QueryTreeView != null)
    {
        this.LstTreeNode.Clear();
        for (int i = 0; i < this.QueryTreeView.Nodes.Count; i++)
        {
            TreeNode node = this.QueryTreeView.Nodes[i];
            if (node != null)
            {
                this.AddTreeNodeToList(node);
            }
        }
    }
}
private void AddTreeNodeToList(TreeNode node)
{
    if (node != null)
    {
        this.LstTreeNode.Add(node);
        for (int i = 0; i < node.Nodes.Count; i++)
        {
            if (node.Nodes[i].Nodes.Count > 0)
            {
                this.AddTreeNodeToList(node.Nodes[i]);
            }
            else
            {
                this.LstTreeNode.Add(node.Nodes[i]);
            }
        }
    }
}

 

posted @ 2013-07-26 15:54  超级塞亚人  阅读(194)  评论(0)    收藏  举报