1
private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
    // Get the node that was clicked
    TreeNode selectedNode = treeView1.HitTest(e.Location).Node;

    if (selectedNode != null)
    {

    }
}

If I have for example a root node:

World

When I click on it I see these nodes:

World
|____ Blue
|____ Green
|____ Red
|____ Black
|____ yellow

If I click on Blue I will see more nodes under Blue for example

World
|____ Blue
|    |____ Day
|    |____ Night
|____ Green
|____ Red
|____ Black
|____ yellow

Now if I click on Blue I will get the selected node name Blue. selectedNode.Name will be Blue

And if I click on Day will get in selectedNode.Name Day But what I want to do is that if I click on Day the selectedNode be Blue\Day or BlueDay

And if under Day there is another node name 1 and I click on 1 so in selectedNode.Name I want to see BlueDay1 or I prefer Blue\Day\1

I want this \\ so I can use it as a directory name. The problem is i'm using the selectedNode.Name as a directory to get files:

List<string> ff = new List<string>();
private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
    TreeNode selectedNode = treeView1.HitTest(e.Location).Node;

    if (selectedNode != null)
    {
        string tt = mainPath + "\\" + selectedNode.Text;
        ff = DirSearch(tt);
        timer1.Enabled = true;
    }
}

If I click on Blue then it's fine it will get all the files under Blue including sub directories. But if i click on 1 and there are files in 1 then it will not get any files since i need the complete path name Blue\Day\1 to get the files from 1.

This is how I'm getting the files

static List<string> DirSearch(string sDir)
{
    List<string> files = new List<string>();
    try
    {
        foreach (string d in Directory.GetDirectories(sDir))
        {
            foreach (string f in Directory.GetFiles(d))
            {
                files.Add(f);
            }
            DirSearch(d);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
    return files;
}

The idea is if I want to get all the files under Blue I click on Blue but if I want to get only the files in 1 when I click on 1 it's not working since 1 is not the complete path.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Abdul Jaja
  • 85
  • 8

1 Answers1

1

Each TreeNode has a FullPath which gets the path from the root tree node to the current tree node. For example you can see full path of all nodes for a tree like below tree:

Tree                Full Path
==============================
1                   1
|__ 11              1\11
    |__ 111         1\11\111
    |__ 112         1\11\112

There is a PathSeparator character for TreeView which gets or sets the delimiter string that the tree node path uses, which is \ by default.

If you want to have more control over the path, you can use Ancestors or AncestorsAndSelf extension methods.

In the example I provided below I've created path without the root node:

var pathWithoutRootNode = string.Join(@"\", 
    e.Node.AncestorsAndSelf().Where(x => x.Parent != null).Select(x => x.Text))
Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • There is no property AncestorsAndSelf of Node. Where should i get it from or how to use it ? Severity Code Description Project File Line Suppression State Error CS1061 'TreeNode' does not contain a definition for 'AncestorsAndSelf' and no extension method 'AncestorsAndSelf' accepting a first argument of type 'TreeNode' could be found (are you missing a using directive or an assembly reference?) – Abdul Jaja Feb 02 '17 at 21:32
  • 1
    I've posted the link of [`AncestorsAndSelf`](http://stackoverflow.com/a/39805732/3110834) method in answer. It's an extension method which I've written [here](http://stackoverflow.com/a/39805732/3110834). – Reza Aghaei Feb 02 '17 at 21:34
  • I added the TreeViewExtensions to my code and removed the static i got errors if it's static. But still in the event AfterSelect this AncestorsAndSelf not exist: e.Node.AncestorsAndSelf() – Abdul Jaja Feb 02 '17 at 21:41
  • `TreeViewExtensions` should be static because it contains extension methods. I've updated the linked code with required `using`s. Just copy the code in `TreeViewExtensions.cs` and use it anywhere in your projects. – Reza Aghaei Feb 02 '17 at 21:52
  • After you made it working, read more about [C# Extension methods](http://msdn.microsoft.com/en/library/bb383977.aspx) which let you to add some methods to a class without inheritance. :) – Reza Aghaei Feb 02 '17 at 21:53
  • Working great. Thank you. – Abdul Jaja Feb 02 '17 at 22:09