Visual Basic Tree View Control not showing hierarchy of items correctly

Experts,

I am trying a small piece of code that should read an array list and populate a tree view control at run time. This could be a bit challenging (and a lot challenging for me :/ )... here is what I want:

My Array list contains a folder path list like below:

local:/ConditionalCallProcessing
local:/ConditionalCallProcessing/xml
local:/ConditionalCallProcessing/xsl
local:/EncryptDecrypt
local:/EncryptDecrypt/xml
local:/EncryptDecrypt/xsl
local:/verisign

I want a treeview control that will show:
1. Local as a root
2. ConditionalCallProcessing as its child.
3. xml and xsl being siblings and children to ConditionalProcessing
4. EncrypDecrypt as child of Local
5. xml and xsl as children to EncryptDecrpt etc.

I also want the node "Local" to show a '+' symbol in front of it. When I wrote a code, the attached files show what I got and what I don't want :(

TIA,
Jay
FoldersNotInHierarchy.jpg
RootNotShowingPlus.jpg
JammerulesAsked:
Who is Participating?
 
Martin LissConnect With a Mentor Older than dirtCommented:
    ' These two settings are needed but they can be set in the IDE
    TreeView1.Style = tvwTreelinesPlusMinusText
    TreeView1.LineStyle = tvwRootLines
    
    ' Add the root and expand the tree
    TreeView1.Nodes.Add , , "local", "local"
    TreeView1.Nodes(1).Expanded = True
    
    ' Add the first child of the root
    TreeView1.Nodes.Add "local", tvwChild, "CCP", "ConditionalCallProcessing"
    
    ' Add two children to that node
    TreeView1.Nodes.Add "CCP", tvwChild, "xml1", "xml"
    TreeView1.Nodes.Add "CCP", tvwChild, "xsl1", "xsl"
    
    ' Add the second child of the root
    TreeView1.Nodes.Add "local", tvwChild, "EncryptDecrypt", "EncryptDecrypt"
    
    ' Add two children to it
    TreeView1.Nodes.Add "EncryptDecrypt", tvwChild, "xml2", "xml"
    TreeView1.Nodes.Add "EncryptDecrypt", tvwChild, "xsl2", "xsl"
    
    ' Add the third child of the root
    TreeView1.Nodes.Add "local", tvwChild, "verisign", "verisign"

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
I don't have a VB IDE available at the moment, but below is the logic you need in C#:
        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < folders.Count; ++i)
            {
                string temp = folders[i].ToString().Replace(":", "");

                string[] values = temp.Split(new char[] { '/' });
                TreeNode parent = null;

                foreach (string s in values)
                {
                    TreeNode[] nodes = treeView1.Nodes.Find(s, true);
                    if (nodes.Length > 0)
                    {
                        parent = nodes[0];
                        continue;
                    }

                    if (parent == null)
                    {
                        parent = treeView1.Nodes.Add(s, s);
                    }
                    else
                        parent.Nodes.Add(s, s);
                }
            }
        }

        private ArrayList folders = new ArrayList() 
        {
            "local:/ConditionalCallProcessing",
            "local:/ConditionalCallProcessing/xml",
            "local:/ConditionalCallProcessing/xsl",
            "local:/EncryptDecrypt",
            "local:/EncryptDecrypt/xml",
            "local:/EncryptDecrypt/xsl",
            "local:/verisign"
        };

Open in new window

Have a crack at replicating it in VB and let me know if you have problems with any specific part and i'll work out the equivalent VB snippet.

Edit: Actually this might not work for you at all. I got to your question via the VB.Net zone - didn't notice until i'd posted that you mainly posted in the VB6 zone. But the logic is still the same, you'll just need to adapt it a bit to fit VB6.
0
 
JammerulesAuthor Commented:
Hello Martin,

I see that you have hardcoded the folder names (e.g., ConditionalCallProcessing etc). I am sorry if I had not mentioned it in my question but I have to do it on the fly. So, in an essence, I have to parse based on ":" and "/". The word before the colon being the root and the word before the "/" is the parent. Thank you!
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Martin LissOlder than dirtCommented:
Where does the information come from? A file, an array, or??? Or maybe you could just post your project in a zip file. If it's too large you can upload it to a site like www.mediafire,com and then post the resulting URL.
0
 
JammerulesAuthor Commented:
It is  coming from a xml file. I have parsed the xml into an array list using the following code:

 readFileDetailsDoc1.Load ("C:\DPAutomation\Output\TargetFileDetails.xml")
        strxpath = "//*/directory"
        Trim (strxpath)
        Set allDirectories = readFileDetailsDoc1.documentElement.selectNodes(strxpath)
        For Each dirctry In allDirectories
          If StrComp(dirctry.nodeName, "directory") = 0 Then
            arrListFilePaths.Add (dirctry.getAttribute("name"))
          End If
        Next

Open in new window

TargetFileDetails.xml
0
 
Martin LissOlder than dirtCommented:
Okay here's a demo project that takes the 7 items you posted and puts them into an array which it then parses. If something like local:/ConditionalCallProcessing/xsl/another exists then you'll need a recursive function (which I'm not good at) instead of what I've written.
Q-28322848.zip
0
 
Martin LissOlder than dirtCommented:
Did that help you?
0
 
JammerulesAuthor Commented:
Not really. I needed to populate it recursively. Here is the code I came up with. But thanks for your quick help.


 Set TreeNode = tvwTargetFolders.Nodes.Add("Root", tvwChild, "local:", "local:")
    TreeNode.Expanded = True
    
    For Each filepath In arrListFilePaths
        arrParsedFilePath = Split(filepath, "/")
        
        For i = 0 To UBound(arrParsedFilePath)
            If StrComp(Trim(TreeNode), Trim(arrParsedFilePath(i))) = 0 Then
                GoTo lbl
            ElseIf TreeNode.Child Is Nothing Then
                Set TreeNode = tvwTargetFolders.Nodes.Add(TreeNode, tvwChild, UniqueKey, arrParsedFilePath(i))
                TreeNode.Expanded = True
                GoTo lbl
            ElseIf StrComp(Trim(TreeNode.Child), Trim(arrParsedFilePath(i))) = 0 Then
                Set TreeNode = tvwTargetFolders.Nodes.Add(TreeNode.Child, tvwChild, UniqueKey, arrParsedFilePath(i + 1))
                TreeNode.Expanded = True
                GoTo lbl
            ElseIf TreeNode.Children <> 0 Then
                For Each TreeNode In tvwTargetFolders.Nodes
                  If StrComp(Trim(TreeNode.Text), arrParsedFilePath(i)) = 0 Then
                    Set TreeNode = tvwTargetFolders.Nodes.Add(TreeNode, tvwChild, UniqueKey, arrParsedFilePath(i + 1))
                    TreeNode.Expanded = True
                    GoTo lbl
                  End If
                Next
            Set TreeNode = tvwTargetFolders.Nodes(2)
            Set TreeNode = tvwTargetFolders.Nodes.Add(TreeNode, tvwChild, UniqueKey, arrParsedFilePath(i))
            TreeNode.Expanded = True
            End If
lbl:
        Next
        Set TreeNode = tvwTargetFolders.Nodes(2)
    Next 

Open in new window


***I am generating a unique key so that there will not be a clash of duplicate nodes with the same name***
 
Private Function UniqueKey() As String
    UniqueKey = "K" & 1 + Int(Rnd() * 10000000)
End Function 

Open in new window

0
 
JammerulesAuthor Commented:
MartinLiss' first response certainly helped me build the code I want. Even though it did not completely help me, I am accepting that as an answer. But for those looking for help with a question similar to mine, my last post might help them too.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.