树视图筛选不返回所有文件 [英] Tree view filtering does not return all the files

查看:89
本文介绍了树视图筛选不返回所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按文件夹和文件填充我的树视图。而我用下面code,以过滤树视图,但它不返回所有匹配的字符串,只是它返回一个文件的文件。
例如字母F中有3个文件,但是当我搜索返回仅有1文件。

 私人树节点FindNodeByValue(TreeNodeCollection节点,字符串搜索字符串)
{    //循环遍历树的节点集合
    的foreach(在节点树节点节点)
    {
        //是否值相匹配的搜索字符串?
        如果(node.Value.ToUpper()。包含(searchstring.ToUpper()))
            //是它不匹配 - 返回它
            返回节点;
        其他
        {
            //没有它不匹配 - 搜索这个节点的子节点
            树节点childNode = SearchChildNodes(节点,搜索字符串);
            //如果在childNode不为空,这是一个比赛
            如果(childNode!= NULL)
                //返回匹配的节点
                返回childNode;
        }
    }
    //如果未找到匹配的节点返回空
    返回null;
}///<总结>
///此方法搜索节点的子节点集合查找匹配的值
与输入搜索字符串///
///它会反复调用自身,因为它钻入每一个节点的子节点(如present)
///< /总结>
///&所述; PARAM NAME =parentNode>父节点以搜索一个匹配&下; /参数>
///< PARAM NAME =搜索字符串>字符串与节点Value属性&LT匹配; /参数>
///<退货和GT;如果发现匹配节点的树节点。如果没有发现这将是空< /回报>
私人树节点SearchChildNodes(树节点parentNode,字符串搜索字符串)
{
    //循环传递的parentNode的子节点
    的foreach(在parentNode.ChildNodes TreeNode的节点)
    {
        //是否值相匹配的搜索字符串?
        如果(node.Value.ToUpper()。包含(searchstring.ToUpper()))
            //是它不匹配 - 返回它
            返回节点;
        其他
        {
            //没有它不匹配 - 递归搜索此节点的子节点
            树节点childNode = SearchChildNodes(节点,搜索字符串);
            //如果在childNode不为空,这是一个比赛
            如果(childNode!= NULL)
                //返回匹配的节点
                返回childNode;
        }
    }
    //如果匹配的节点没有找到或者如果没有子节点,则返回null
    返回null;
}
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
    树节点trnode = FindNodeByValue(TreeView1.Nodes,fieldFilterTxtBx.Text);
    如果(trnode!= NULL)
    {
        TreeView1.Nodes.Clear();
      //树节点newnode =新的TreeNode(细节工程);
       // TreeView1.Nodes.Add(newnode);
        TreeView1.Nodes.Add(trnode);
        TreeView1.ExpandAll();
    }
    其他{Label1.Text =没有找到文件;}


解决方案

它只能返回一个值,因为如果没有文件找到该功能只递归调用自己。您需要将递归添加到SearchChildNodes if语句的这个分支,以及:

 如果(node.Value.ToUpper()。包含(searchstring.ToUpper()))
//是它不匹配 - 返回它
    返回节点;
其他

您可能还需要将文件名添加到一个数组或Generic.List,以多个存储在一个时间。

My tree view populated by folders and files. And I have used below code for filtering tree view but it does not return all the files that match string, just it returns one file. For example letter "f" there is in 3 files but when I search it returns just 1 file.

private TreeNode FindNodeByValue(TreeNodeCollection nodes, string searchstring)
{

    // Loop through the tree node collection
    foreach (TreeNode node in nodes)
    {
        // Does the value match the search string?
        if (node.Value.ToUpper().Contains (searchstring.ToUpper()))
            // Yes it does match - return it
            return node;
        else
        {
            // No it does not match - search any child nodes of this node
            TreeNode childNode = SearchChildNodes(node, searchstring);
            // If the childNode is not null it was a match
            if (childNode != null)
                // Return the matching node
                return childNode;
        }
    }
    // If the matching node is not found return null
    return null;
}

/// <summary>
/// This method searches a node's ChildNodes collection to find a matching value
/// with the incoming search string
/// It will iteratively call itself as it drills into each nodes child nodes (if present)
/// </summary>
/// <param name="parentNode">Parent node to search for a match</param>
/// <param name="searchstring">string to be matched with the Nodes Value property</param>
/// <returns>Treenode of the matching node if found.  If not found it will be null</returns>
private TreeNode SearchChildNodes(TreeNode parentNode, string searchstring)
{
    // Loop through the child nodes of the parentNode passed in
    foreach (TreeNode node in parentNode.ChildNodes)
    {
        // Does the value match the search string?
        if (node.Value.ToUpper().Contains(searchstring.ToUpper()))
            // Yes it does match - return it
            return node;
        else
        {
            // No it does not match - recursively search any child nodes of this node
            TreeNode childNode = SearchChildNodes(node, searchstring);
            // If the childNode is not null it was a match
            if (childNode != null)
                // Return the matching node
                return childNode;
        }
    }
    // If the matching node is not found OR if there were no child nodes then return null
    return null;
}


protected void Button1_Click(object sender, EventArgs e)
{
    TreeNode trnode=FindNodeByValue(TreeView1.Nodes, fieldFilterTxtBx.Text);
    if (trnode != null)
    {
        TreeView1.Nodes.Clear();
      //  TreeNode newnode = new TreeNode("Detail Engineering");
       // TreeView1.Nodes.Add(newnode);
        TreeView1.Nodes.Add(trnode);
        TreeView1.ExpandAll();
    }
    else

{

Label1.Text = "No file found";

}

解决方案

It is only returning one value because the function only calls itself recursively if no file is found. You need to add the recursion to this branch of the if statement in SearchChildNodes as well:

if (node.Value.ToUpper().Contains(searchstring.ToUpper()))
// Yes it does match - return it
    return node;
else

You also may need to add the filenames to an array or a Generic.List in order to store more than one at a time.

这篇关于树视图筛选不返回所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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