Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t在C中使用新节点从TreeView收集现有节点#_C#_C# 4.0_Treeview - Fatal编程技术网

C# Can';t在C中使用新节点从TreeView收集现有节点#

C# Can';t在C中使用新节点从TreeView收集现有节点#,c#,c#-4.0,treeview,C#,C# 4.0,Treeview,我有以下功能,可以在树视图中搜索: public static TreeNode FindAllNamesInTreeView(TreeView treeView, String name, int StartNode = -1, bool Searchilds = true) { TreeNode newNode = new TreeNode("SRes"); newNode.Nodes.Add("Test"); // check if we have a treeview

我有以下功能,可以在树视图中搜索:

public static TreeNode FindAllNamesInTreeView(TreeView treeView, String name, int StartNode = -1, bool Searchilds = true)
{
   TreeNode newNode = new TreeNode("SRes");
   newNode.Nodes.Add("Test");

   // check if we have a treeview
   if (treeView == null)
      return null;

   // iterate through the treeview's root nodes
   for (int i = 0; i < treeView.Nodes.Count; i++)
   {
      // for each root node try to find the node with the name we want
      TreeNode foundNode = FindNameInTreeView(treeView.Nodes[i], name, StartNode, Searchilds);

      // if we found the node, return it
      if (foundNode != null)
         if (TheIndexOf(foundNode) > StartNode)
            newNode.Nodes.Add( foundNode); //Error here!
   }

   // no node found
   return newNode;
}
公共静态树节点FindAllNamesInTreeView(树视图树视图,字符串名称,int StartNode=-1,bool Searchilds=true)
{
TreeNode newNode=新的TreeNode(“SRes”);
newNode.Nodes.Add(“测试”);
//看看我们有没有树景
if(treeView==null)
返回null;
//遍历树视图的根节点
for(int i=0;i开始节点)
newNode.Nodes.Add(foundNode);//此处出错!
}
//找不到节点
返回newNode;
}
在执行newNode.Add(foundNode)时;我有以下例外情况:

public static TreeNode FindAllNamesInTreeView(TreeView treeView, String name, int StartNode = -1, bool Searchilds = true)
{
   TreeNode newNode = new TreeNode("SRes");
   newNode.Nodes.Add("Test");

   // check if we have a treeview
   if (treeView == null)
      return null;

   // iterate through the treeview's root nodes
   for (int i = 0; i < treeView.Nodes.Count; i++)
   {
      // for each root node try to find the node with the name we want
      TreeNode foundNode = FindNameInTreeView(treeView.Nodes[i], name, StartNode, Searchilds);

      // if we found the node, return it
      if (foundNode != null)
         if (TheIndexOf(foundNode) > StartNode)
            newNode.Nodes.Add( foundNode); //Error here!
   }

   // no node found
   return newNode;
}
System.Windows.Forms.dll中发生“System.ArgumentException”类型的首次意外异常


有谁能告诉我出了什么问题,或者我如何将所有找到的节点添加到一个树中?

您不能将同一个节点添加到两个树中。因此,如果您找到一个,并尝试将其添加到
newNode
,您将得到一个
ArgumentException


我建议不要使用
TreeNode
,而是返回
列表

我希望找到所有节点的集合。将来,我希望迭代它们以实现FindNext函数。Decision found-我将使用列表存储找到的元素: