This is a migrated thread and some comments may be shown as answers.

Delimiter-separated string to RadTreeView VB.NET

2 Answers 27 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Jiten
Top achievements
Rank 1
Jiten asked on 03 Jan 2017, 01:00 PM

I have an array of paths separated by a delimiter:

Dim paths = New List(Of String)() From {
     "C:\WINDOWS\AppPatch\MUI\040C",
     "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
     "C:\WINDOWS\Microsoft.NET\Framework\addins\MUI",
     "C:\WINDOWS\addins",
     "C:\WINDOWS\AppPatch",
     "C:\WINDOWS\AppPatch\MUI",
     "C:\WINDOWS\Microsoft.NET\Framework\MUI\MUI\0409"
 }

 

And I want to create a RadTreeView that will look something like this:

+C:
    +Windows
        +AppPatch
        +addins
        +Microsoft.NET
            +Framework
...

 

This is what I have managed to do until now but there's something I'm missing:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
    Dim paths = New List(Of String)() From {
        "C:\WINDOWS\AppPatch\MUI\040C",
        "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
        "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
        "C:\WINDOWS\addins",
        "C:\WINDOWS\AppPatch",
        "C:\WINDOWS\AppPatch\MUI",
        "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
    }
 
    If Not Page.IsPostBack Then
        Dim subPathAgg As String
        Dim pathSeparator As String = "\"
 
        ' For each complete individual path
        For Each path As String In paths
 
            subPathAgg = String.Empty
 
            ' Fill array of strings with each delimited part
            Dim arrFolders As List(Of String) = (path).Split(pathSeparator).ToList
            Dim lastNode As RadTreeNode = Nothing
 
            Dim iCount As Integer = 0
 
            'For each one of the folders
            For Each folder As String In path.Split(pathSeparator)
                subPathAgg += folder & pathSeparator
 
                Dim foundNode As RadTreeNode = RadTreeView1.Nodes.FindNodeByValue(subPathAgg, True)
                If foundNode Is Nothing Then
                    If lastNode Is Nothing Then
                        lastNode = New RadTreeNode(folder, subPathAgg)
                        RadTreeView1.Nodes.Add(lastNode)
                    Else
                        Dim otherNode = New RadTreeNode(folder, subPathAgg)
                        lastNode.Nodes.Add(otherNode)
                        lastNode = otherNode
                    End If
                Else
                    If foundNode.Text <> folder Then
                        Dim otherNode = New RadTreeNode(folder, subPathAgg)
                        foundNode.Nodes.Add(otherNode)
                        lastNode = otherNode
                    End If
                End If
 
            Next
            lastNode = Nothing
 
        Next
 
    End If
 
End Sub

 

This is how the RadTreeView looks like right now:

2 Answers, 1 is accepted

Sort by
0
Accepted
Konstantin Dikov
Telerik team
answered on 04 Jan 2017, 03:42 PM
Hello Jiten,

This is more complex task and you need to take into account all of the existing paths. For your convenience, following is a C# and VB examples for such implementation:
private void AddNode(RadTreeView treeView, string path)
 {
     List<string> folders = GetPaths(path);
     folders.Reverse();
     for (int i = 0; i < folders.Count; i++)
     {
         RadTreeNode node = treeView.FindNodeByValue(folders[i]);
         string folderName = GetCurrentFolder(folders[i]);
         if (node == null)
         {
             if (i == 0)
             {
                 treeView.Nodes.Add(new RadTreeNode(folderName, folders[i]));
             }
             else
             {
                 treeView.FindNodeByValue(folders[i - 1]).Nodes.Add(new RadTreeNode(folderName, folders[i]));
             }
         }
         else
         {
             if (node.Text != folderName)
             {
                 node.Nodes.Add(new RadTreeNode(folderName, folders[i]));
             }
         }
     }
 }
 
 private string GetCurrentFolder(string path)
 {
     var folders = path.Split(new string[] { "\\" }, StringSplitOptions.None);
     return folders[folders.Count() - 1];
 }
 
 private List<string> GetPaths(string path)
 {
     List<string> paths = new List<string>();
     paths.Add(path);
     while (path.LastIndexOf("\\") >= 0)
     {
         path = path.Substring(0, path.LastIndexOf("\\"));
         paths.Add(path);
     };       
     return paths;
 }
 
 protected void Page_Load(object sender, System.EventArgs e)
 {
 
     dynamic paths = new List<string> {
     "C:\\WINDOWS\\AppPatch\\MUI\\040C",
     "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727",
     "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\MUI",
     "C:\\WINDOWS\\addins",
     "C:\\WINDOWS\\AppPatch",
     "C:\\WINDOWS\\AppPatch\\MUI",
     "C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\MUI\\0409"
 };
 
     if (!Page.IsPostBack)
     {
         foreach (string path in paths)
         {
             AddNode(RadTreeView1, path);
         }
     }
 }

And the VB:
Private Sub AddNode(treeView As RadTreeView, path As String)
    Dim folders As List(Of String) = GetPaths(path)
    folders.Reverse()
    For i As Integer = 0 To folders.Count - 1
        Dim node As RadTreeNode = treeView.FindNodeByValue(folders(i))
        Dim folderName As String = GetCurrentFolder(folders(i))
        If node Is Nothing Then
            If i = 0 Then
                treeView.Nodes.Add(New RadTreeNode(folderName, folders(i)))
            Else
                treeView.FindNodeByValue(folders(i - 1)).Nodes.Add(New RadTreeNode(folderName, folders(i)))
            End If
        Else
            If node.Text <> folderName Then
                node.Nodes.Add(New RadTreeNode(folderName, folders(i)))
            End If
        End If
    Next
End Sub
 
Private Function GetCurrentFolder(path As String) As String
    Dim folders = path.Split(New String() {"\"}, StringSplitOptions.None)
    Return folders(folders.Count() - 1)
End Function
 
Private Function GetPaths(path As String) As List(Of String)
    Dim paths As New List(Of String)()
    paths.Add(path)
    While path.LastIndexOf("\") >= 0
        path = path.Substring(0, path.LastIndexOf("\"))
        paths.Add(path)
    End While
     
 
    Return paths
End Function
 
Protected Sub Page_Load(sender As Object, e As System.EventArgs)
 
    Dim paths As dynamic = New List(Of String)() From { _
        "C:\WINDOWS\AppPatch\MUI\040C", _
        "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727", _
        "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI", _
        "C:\WINDOWS\addins", _
        "C:\WINDOWS\AppPatch", _
        "C:\WINDOWS\AppPatch\MUI", _
        "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409" _
    }
 
    If Not Page.IsPostBack Then
        For Each path As String In paths
            AddNode(RadTreeView1, path)
        Next
    End If
End Sub


Best Regards,
Konstantin Dikov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Jiten
Top achievements
Rank 1
answered on 05 Jan 2017, 08:33 AM
Thank you very much Konstantin! It's working perfectly.
Tags
TreeView
Asked by
Jiten
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Jiten
Top achievements
Rank 1
Share this question
or