【问题标题】:C# TreeView search function implementationC# TreeView 搜索功能实现
【发布时间】:2019-10-08 09:02:18
【问题描述】:

我目前正在尝试在我正在进行的一个小项目中实现搜索功能。关于该项目的一些背景知识,我有一个 xml,我从中导入一些存储在树视图中的数据。我的目标是能够给出一个字符串并在树视图中仅显示包含该特定字符串的节点。

对于下面的 xml,我的树视图看起来像这样

 Breakfast
     Belgian waffles
     Strawberry Belgian Waffles 

如果我选择一个节点,我有一个带有一些文本框的组框,其中包含每个项目的属性(价格、卡路里等)

我希望我的函数能够搜索任何东西......即,如果我给出价格,则以该价格在树视图中显示项目/项目,如果我搜索华夫饼以显示所有华夫饼。

<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>
      Two of our famous Belgian Waffles with plenty of real maple syrup
    </description>
    <calories>650</calories>
  </food>
  <food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>
      Light Belgian waffles covered with strawberries and whipped cream
    </description>
    <calories>900</calories>
  </food>
</breakfast_menu>

我有一个递归函数,但在第 3 行(TreeNode t = treeNode.FirstNode)它会弹出一个空引用异常。我的猜测是,当它命中一个没有子节点的节点时会弹出这个异常,但我现在不知道如何处理它。

private void IterateRecursive(TreeNode treeNode)
        {
            TreeNode t = treeNode.FirstNode;
            if (t!=null)
            {
                foreach (TreeNode tn in treeNode.Nodes)
                {
                    IterateRecursive(tn);
                }
            }
            if (!treeNode.Text.Contains(textBox_search_string.ToString()))
            {
                treeView1.Nodes.Remove(treeNode);
            }
        }

此外,如果我可以通过其他方式实现此搜索功能,我愿意接受建议。

【问题讨论】:

    标签: c# search treeview


    【解决方案1】:
         public static void Search(string s)
            {
              bool node_add_decision = false;
              int i = 0;
              foreach (XmlNode p_node in xml_list)
              {
                    i++;
                    node_add_decision = false;
                    if (p_node.Attributes.Count > 0)
                    {
                        foreach (XmlAttribute attr in p_node.Attributes)
                        {
                            if (attr.Name.Contains(s) || attr.Value.Contains(s))
                            {
                                node_add_decision = true;
                            }
                        }                  
                        if (!node_add_decision)
                        {
                            node_add_decision=search_c(p_node, s);
                        }
                    }
                    if (node_add_decision)
                    {
                        Add_search_list(p_node,i);
                    }
                 }
             }
    
      private static bool search_c(XmlNode xn, string s)
        {
          if (xn.HasChildNodes)
                {
                    foreach (XmlNode chld in xn.ChildNodes)
                    {
                            for (int i = 0; i <= chld.ChildNodes.Count - 1; i++)
                            {
                                if(chld.ChildNodes[i].Value!=null)
                                {
                                    if (chld.ChildNodes[i].Value.Contains(s))
                                        return true;
                                }
                                if (chld.ChildNodes[i].Name.Contains(s))
                                {
                                    return true;
                                }
                                else {
                                    if (chld.ChildNodes[i].Attributes.Count > 0)
                                    {
                                        foreach (XmlAttribute attr in chld.ChildNodes[i].Attributes)
                                        {
                                            if (attr.Name.Contains(s) || attr.Value.Contains(s))
                                            {
                                                return true;
                                            }
                                        }
                                    }                            
                            }
                        }
                    }
                }
                return false;
            }
    

    所以,我得到了名为 Search 的主函数,对于列表中的每个节点,它将在它的所有属性中搜索所需的字符串。如果找到,则 node_add_decision 将设置为 true,这意味着该节点包含字符串,不再需要搜索它的子节点。如果在当前节点中没有字符串的痕迹,则节点决策将是错误的,意味着我们必须搜索它的子节点。

    search_c是通过childs搜索字符串的函数。对于每个子节点我们搜索字符串,如果找到返回true,否则返回false。

    对于我的情况,我必须显示它不包含字符串的事件,但它的子项包含。如果不是你的情况,你可以放弃 search_c 函数和代码的节点决策部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 2021-01-08
      • 2019-02-13
      相关资源
      最近更新 更多