Find and check checkbox of a node in a treeview

0

I have a treeview with all the directory and file structure of the PC where the application is running. When I start, I get a list of strings with several routes, for which I need to find the treeview node corresponding to each of them and mark their checkbox.

The problem is that at the beginning of the application, only the units (fixed and removable disks) appear in the treeview and as I expand each node it is completed with its subdirectories (this is what is proposed so as not to overload the beginning of the application). So what I do not find a way to do is, first, expand the nodes until you reach the last level of each route (it can be a file or directory), and second, check your checkbox.

The code I have is the following:

When starting the application:

foreach (DriveInfo unidad in DriveInfo.GetDrives())
{
    if (unidad.DriveType != DriveType.Fixed && unidad.DriveType != 
        DriveType.Removable)
    {
        continue;
    }

    TreeNode raiz = new TreeNode(unidad.Name);
    raiz.ToolTipText = "Unidad";
    raiz.Tag = unidad.Name;
    raiz.ImageIndex = 0;

    raiz.Nodes.Add(new TreeNode());
    tvArbol.Nodes.Add(raiz);                                                
}

When expanding a node:

DirectoryInfo directorioPadre = new DirectoryInfo(path);
DirectoryInfo[] subDirectorios = directorioPadre.GetDirectories();

foreach (DirectoryInfo subDirectorio in subDirectorios)
{
    TreeNode directorioHijo = new TreeNode(subDirectorio.Name);
    directorioHijo.Tag = subDirectorio.FullName;
    directorioHijo.ImageIndex = 1;

    directorioHijo.Nodes.Add(new TreeNode());
    directorioHijo.Checked = nodo.Checked;
    nodo.Nodes.Add(directorioHijo);
}

List<FileInfo> archivos = new List<FileInfo>();
archivos.AddRange(directorioPadre.GetFiles());

foreach (FileInfo archivo in archivos)
{
    TreeNode archivoHijo = new TreeNode(archivo.Name);
    archivoHijo.ImageIndex = 2;

    archivoHijo.Tag = archivo;
    archivoHijo.Checked = nodo.Checked;
    nodo.Nodes.Add(archivoHijo);
}
    
asked by Willy616 12.07.2017 в 14:28
source

1 answer

1

You say you have a List<string> with the paths of several files. Then I understand that you have to mark as Checked the routes that coincide as you expand them.

Here is a method that checks whether the Checked should be added to a node. Sure it can be optimized and I would probably make it an extension method, but to give you an idea ( paths is the list of routes, change it for yours) .Use the Level of the node to know until which part of the path you have to compare:

private bool isChecked(TreeNode node)
{
    bool check= false;
    foreach (string pathcomparar in paths)
    {
        string[] splittedPath = pathcomparar.Split(new char[] { '\' }, StringSplitOptions.RemoveEmptyEntries);
        string[] splittedTreePath = node.FullPath.Split(new char[] { '\' }, StringSplitOptions.RemoveEmptyEntries);
        if (String.Join("\", splittedPath.Take(node.Level + 1)) == String.Join("\", splittedTreePath.Take(node.Level + 1)))
        {
            check = true;
        }
    }
    return check;
}

With this method, you only have to modify your code in the following way.

When starting the application:

foreach (DriveInfo unidad in DriveInfo.GetDrives())
{
    if (unidad.DriveType != DriveType.Fixed && unidad.DriveType !=
        DriveType.Removable)
    {
        continue;
    }

    TreeNode raiz = new TreeNode(unidad.Name);
    raiz.ToolTipText = "Unidad";
    raiz.Tag = unidad.Name;
    raiz.ImageIndex = 0;

    raiz.Nodes.Add(new TreeNode());
    tvArbol.Nodes.Add(raiz);
    raiz.Checked = isChecked(raiz);
}

When expanding nodes (I understand that you use the AfterExpand event):

DirectoryInfo directorioPadre = new DirectoryInfo(path);
DirectoryInfo[] subDirectorios = directorioPadre.GetDirectories();

foreach (DirectoryInfo subDirectorio in subDirectorios)
{
    TreeNode directorioHijo = new TreeNode(subDirectorio.Name);
    directorioHijo.Tag = subDirectorio.FullName;
    directorioHijo.ImageIndex = 1;

    directorioHijo.Nodes.Add(new TreeNode());
    //directorioHijo.Checked = nodo.Checked;
    nodo.Nodes.Add(directorioHijo);

    directorioHijo.Checked = isChecked(directorioHijo);
}

List<FileInfo> archivos = new List<FileInfo>();
archivos.AddRange(directorioPadre.GetFiles());

foreach (FileInfo archivo in archivos)
{
    TreeNode archivoHijo = new TreeNode(archivo.Name);
    archivoHijo.ImageIndex = 2;

    archivoHijo.Tag = archivo;
    nodo.Nodes.Add(archivoHijo);

    archivoHijo.Checked = isChecked(archivoHijo);
}

If you have any questions or problems, tell me about it.

    
answered by 12.07.2017 / 17:23
source