Solved

Visual Basic Tree View Control not showing hierarchy of items correctly

Posted on 2013-12-19
9
510 Views
Last Modified: 2013-12-30
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
0
Comment
Question by:Jammerules
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39731438
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
 
LVL 48

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 39732405
    ' 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
 

Author Comment

by:Jammerules
ID: 39732450
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 48

Expert Comment

by:Martin Liss
ID: 39732464
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
 

Author Comment

by:Jammerules
ID: 39732471
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
 
LVL 48

Expert Comment

by:Martin Liss
ID: 39733005
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
 
LVL 48

Expert Comment

by:Martin Liss
ID: 39743702
Did that help you?
0
 

Author Comment

by:Jammerules
ID: 39745605
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
 

Author Comment

by:Jammerules
ID: 39745608
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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question