Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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# 如何在树视图中查找根文件夹的索引?_C# - Fatal编程技术网

C# 如何在树视图中查找根文件夹的索引?

C# 如何在树视图中查找根文件夹的索引?,c#,C#,如何在树视图中查找每个根文件夹的索引? 假设有一个具有4个根节点的树视图。它们都位于同一级别,并且都没有显示子节点: |-a |-b |-c |-d 现在让我们假设在c根节点的分支上有一个选定的节点。如何获取c节点的索引?在本例中,它是根节点之间的第三个节点 因此,给定一个选定的节点,如何获取其根节点的索引?要完成您的任务,您应该找到单击的节点的父节点,然后是父节点的父节点等。。。所以,我们需要一个递归 查看带有注释的示例代码: private void Form1_Load(object s

如何在树视图中查找每个根文件夹的索引? 假设有一个具有4个根节点的树视图。它们都位于同一级别,并且都没有显示子节点:

|-a
|-b
|-c
|-d
现在让我们假设在c根节点的分支上有一个选定的节点。如何获取c节点的索引?在本例中,它是根节点之间的第三个节点


因此,给定一个选定的节点,如何获取其根节点的索引?

要完成您的任务,您应该找到单击的节点的父节点,然后是父节点的父节点等。。。所以,我们需要一个递归

查看带有注释的示例代码:

private void Form1_Load(object sender, EventArgs e)
{
    //add test data on form load (you can do it on form design, too.
    //there are 4 root nodes and each of them has one subnode. 
    //Additionally, c's first node, called 'c-1', has it's own child.
    treeView1.Nodes.Add(new TreeNode("a"));
    treeView1.Nodes.Add(new TreeNode("b"));
    treeView1.Nodes.Add(new TreeNode("c"));
    treeView1.Nodes.Add(new TreeNode("d"));
    treeView1.Nodes[0].Nodes.Add(new TreeNode("a-1"));
    treeView1.Nodes[1].Nodes.Add(new TreeNode("b-1"));
    treeView1.Nodes[2].Nodes.Add(new TreeNode("c-1"));
    treeView1.Nodes[3].Nodes.Add(new TreeNode("d-1"));
    treeView1.Nodes[2].Nodes[0].Nodes.Add(new TreeNode("c-1-1"));

    //expand those nodes to see things clearly
    treeView1.ExpandAll();

    //subscribe to after select event. When user selects one node, treeView1_AfterSelect method will be called.
    //this can be done on form designer too, on properties panel
    treeView1.AfterSelect += treeView1_AfterSelect;

}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    //this method will be called when you select node
    //find topmost parent by calling method FindTopMostParent and passing it selected node 
    var topmostParent = FindTopMostParent(e.Node);

    //here's your index of topmost node (parent or grandparent or grand-grand of selcted node(
    var index = treeView1.Nodes.IndexOf(topmostParent);

}

private TreeNode FindTopMostParent(TreeNode node)
{
    //first, we check if passed node has parent. If not, return that node, because it's the topmost one
    //then, repeat that search for parent again and again until you find that node which does not have parent
    while (node.Parent != null)
        return FindTopMostParent(node.Parent);

    //return parentless node :)
    return node;
}

WinForms?wpf?asp.net mvc?WinForms。问题是我用根文件夹列表填充了一个树状视图,然后它们的子文件夹被添加为子节点。但只有它们的名字存储在treeview中。我必须从所选节点重建完整路径。因此,我必须将正确的根文件夹添加到所选节点的完整路径,以便创建完整路径。这就是为什么我必须知道每个根节点的索引,以便将它们与列表中相应的根文件夹配对。共享您迄今为止所做工作的代码。@MAdeelKhalid在执行SELECT此语句后选择节点时:selectedFolder=rootFoldersList[index]+e.node.FullPath;我需要找到属于当前节点的根节点的索引,才能使用它找到正确的根文件夹。Nino see在下面找到了这个问题的解决方案。谢谢,这就是我想要的。