Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Trees drawing
Hello to all,
 
so this may sound a little weird question but i looked for it and could not found any helpful information.
 
I would like to draw a Tree or Hierarchical diagram in Visual Studio (C#). I have an XML file where i have parent and child nodes. I would read the xml and for each node i would like to draw a box or triangle or so. I should be able to click on that triangle because i would like to present further information regarding a particular node. Does anybody know any useful links which can guide me how to achieve this.
 

I used MSChart Control in visual studio and its wonderful but the only problem is, it does not provide a way to draw Hierarchical diagrams like Tree or so.
 
thanks in advance.
Posted 3-Oct-12 2:42am
Comments
n.podbielski at 3-Oct-12 9:06am
   
Are you trying to make VS add in? Or you using VS as IDE.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
Your question seems a bit vague but interesting, I would recommend using a
treeview becuase it supports nodes and images like you said.
 
            //First lets read the xml..
            List<treenode> parents = new List<treenode>();
            List<treenode> nodes = new List<treenode>(); //Create a storage for xml nodes
            string[] temp = new string[256]; //Helper for organizing nodes
            XmlTextReader tr = new XmlTextReader(File.Open(@"C:/Users/Austin/Desktop/example.xml", FileMode.Open)); //Create Xml Reader
            while (tr.Read())
            {
                if (tr.NodeType == XmlNodeType.Element && !tr.HasValue)
                    parents.Add(new TreeNode("<" + tr.Name + ">")); //Parent nodes
                if (tr.NodeType == XmlNodeType.Element && tr.HasValue)
                    temp[tr.LineNumber] = "<" + tr.Name + ">"; //Child nodes                   
                if (tr.NodeType == XmlNodeType.Text)
                    nodes.Add(new TreeNode((temp[tr.LineNumber] ?? "") + tr.Value));
            }
            tr.Close();
 
            ImageList list = null; // Implement your images here...
            treeView1.ImageList = list;
 
            TreeNode[] collection = new TreeNode[parents.Count];
            for (int i = 0; i < parents.Count; i++)
            {
                collection[i] = new TreeNode(parents[i].Text, nodes.ToArray()); //Combine parents and child nodes
                collection[i].ImageIndex = 1; //Index of imagelist to show
            }
 
            treeView1.Nodes.AddRange(collection); //Add the collection to your treeview
  Permalink  
v2
Comments
imran.mm7 at 10-Oct-12 7:14am
   
Thanks for your reply, I understand the first part of your solution where you propose to read the xml file and extract the information to a treeview Control.
 
Second part of your solution suggests to read the collection of nodes and use the ImageList control to add specific images (at-least what i think).
 
but here is my problem again, hopefully this time its not vague.
 
I can read the xml file, I can use the treeview control and put the information from xml file into parent and child nodes and the tree is populated.
 
The second step is to draw this tree in visual shape. e.g., Using a drawing control. but simple drawing does not help. let me explain it further. once i get the information from xml file and populate the tree. I want to print the tree in graphical shape where i can click the individual elements of the graph (e.g. triangle or button) and when i click it i would like to open a dialogue box or something which shows further semantic information about the element. I am looking forward if there is any control which let me draw the tree in graphical shape and let me select the individual items.
 
thanks in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 220
2 DamithSL 190
3 Raul Iloc 185
4 Richard MacCutchan 175
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100