Solved

Visual Basic Tree View Control not showing hierarchy of items correctly

Posted on 2013-12-19
9
499 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
  • 4
  • 4
9 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
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 45

Accepted Solution

by:
Martin Liss earned 500 total points
Comment Utility
    ' 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
Comment Utility
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
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Jammerules
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
Did that help you?
0
 

Author Comment

by:Jammerules
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now