Author Topic: Building a treeview with recursion  (Read 13263 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Building a treeview with recursion
« Reply #15 on: March 19, 2009, 03:43:57 PM »
I was thinking of using regex myself.
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Building a treeview with recursion
« Reply #16 on: March 19, 2009, 03:48:02 PM »
I was thinking of using regex myself.
Never mind ...
TheSwamp.org  (serving the CAD community since 2003)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Building a treeview with recursion
« Reply #17 on: March 19, 2009, 04:26:55 PM »
Tim I appreciate your time, I was able toactually understand what you were doing and believe it or not, I was pretty close already .. my incrementing wasn't done correctly and a couple of other minor things. Thanks for helping me get over the hump ...

Incidently, I decided to use the streamreader as opposed to reading the whole file up front ... this will prevent future problems should the files get very big.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Building a treeview with recursion
« Reply #18 on: March 19, 2009, 04:28:49 PM »
You're welcome Keith.  I haven't gotten a chance to think C# in a while, and it was fun to get my feet in again.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Building a treeview with recursion
« Reply #19 on: March 19, 2009, 04:36:07 PM »
Lately I have been thinking in all sorts of languages ... there hasn't been much to draw .. no houses are selling, so my boss has me automating much of the daily tasks around the office ... I've had to work in just about everything from html to C++ ... depending upon the needs at the time .. not much AutoCAD though ;)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Building a treeview with recursion
« Reply #20 on: March 19, 2009, 04:48:48 PM »
Sometimes I'm thankful I'm not in the private sector of drawings, so I've been pretty consistently busy, which is nice, but doesn't leave much time for programming, as I'm just a monkey.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Maverick®

  • Seagull
  • Posts: 14778
Re: Building a treeview with recursion
« Reply #21 on: March 19, 2009, 04:49:25 PM »
Lately I have been thinking in all sorts of languages ...

Klingon?

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Building a treeview with recursion
« Reply #22 on: March 19, 2009, 04:54:08 PM »
Lately I have been thinking in all sorts of languages ...

Klingon?

Funny you should mention that .. I actually found out that I could study Klingon at the local university ... evidently there is a huge demand now for people who can speak it to deal with inmates at any one of the myriad of mental health facilities.

I did look into it ... but for me it would be a novelty ... so I didn't pursue it ;)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

tjr

  • Guest
Re: Building a treeview with recursion
« Reply #23 on: March 19, 2009, 10:58:51 PM »
Keith:

I did something like this a while ago as a test of sorts. This should get you started (just ignore the filehandler stuff, that was C++ code)

Code: [Select]
       private void btnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                // Create an OpenFileDialogBox to select the text file to read.
                this.ofdFilePicker.InitialDirectory = Environment.CurrentDirectory;
                this.ofdFilePicker.Filter = "text files (*.txt)|*.txt";
                if (this.ofdFilePicker.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        // Create a new XML document.
                        XmlDocument doc = new XmlDocument();
                        // Read the contents of a file using the C++ file reader
                        // and load it into the XMLDoc.
                        string xmlDoc = fileHandler.readFile(this.ofdFilePicker.FileName);
                        doc.LoadXml(xmlDoc);
                        // Clear the treeview nodes and then start adding nodes too
                        // it from the file.
                        this.tvXMLViewer.Nodes.Clear();
                        TreeNode rootNode = this.tvXMLViewer.Nodes.Add(doc.DocumentElement.Name);
                        // Call our TreeClimber function to traverse the XML nodes and
                        // populate the TreeView
                        this.xmlTreeClimber(doc.DocumentElement, rootNode);
                        this.tvXMLViewer.ExpandAll();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: Could not generate a treeview from the specified XML document. Original error: " + ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read XML file. Original error: " + ex.Message);
            }
        }

        private void xmlTreeClimber(System.Xml.XmlNode node, TreeNode treeNode)
        {
            // Process the child nodes if available.
            if (node.HasChildNodes)
            {
                // Loop through all the child nodes.
                XmlNodeList nodeList = node.ChildNodes;
                for (int count = 0; count <= nodeList.Count - 1; count++)
                {
                    XmlNode newXNode = node.ChildNodes[count];
                    treeNode.Nodes.Add(new TreeNode(newXNode.Name));
                    TreeNode newTNode = treeNode.Nodes[count];
                    // recursively call xmlTreeClimber.
                    xmlTreeClimber(newXNode, newTNode);
                }
            }
            else
            {
                // If no children use trimmed OuterXML.
                treeNode.Text = node.OuterXml.Trim();
            }
        }
        private void XmlTreeBuilder(TreeNodeCollection tnCollection)
        {
            // Loop through all nodes in the collection.
            foreach (TreeNode node in tnCollection)
            {
                // If the node has children recursivley call XmlTreeBuilder.
                if (node.Nodes.Count > 0)
                {
                    xmlWriter.WriteStartElement(node.Text);
                    XmlTreeBuilder(node.Nodes);
                    xmlWriter.WriteEndElement();
                }
                else
                {
                    // If no children just write the text.
                    xmlWriter.WriteString(node.Text);
                }
            }
        }

MikeTuersley

  • Guest
Re: Building a treeview with recursion
« Reply #24 on: March 20, 2009, 05:18:34 PM »
You guys are sure going about this the long way. This is something relatively easy to do with only a minor amount of programming required. Here's how to approach it:

1. Use an xml file instead of a text file. Take an existing text file and convert it by adding the xml tags you want:
    <Level1>Floor System (main)</Level1>
   <Level2>Floor truss system</Level2>
      <etc.>
2. Use xsd.exe [in the Framework folders] and generate an xsd file from your xml file
3. Use xsd.exe again to generate a c# class from the new xsd file
4. Attach the class to your project
5. Now you can use serilization to open and save the data [assuming you are using base data types]
6. Once it is opened and populated your object, your tree code becomes a simple nested For loop structure:
    pseudo code:
   For each Level1 item
      For each level2 item
         for each levelX item
             

TonyT

  • Guest
Re: Building a treeview with recursion
« Reply #25 on: March 20, 2009, 10:28:38 PM »
Very good Mike.  I see you've learned something.

XSD is not really needed in this case, unless the data being persisted is represented by classes in code, and there's additional information associated with each node.

You guys are sure going about this the long way. This is something relatively easy to do with only a minor amount of programming required. Here's how to approach it:

1. Use an xml file instead of a text file. Take an existing text file and convert it by adding the xml tags you want:
    <Level1>Floor System (main)</Level1>
   <Level2>Floor truss system</Level2>
      <etc.>
2. Use xsd.exe [in the Framework folders] and generate an xsd file from your xml file
3. Use xsd.exe again to generate a c# class from the new xsd file
4. Attach the class to your project
5. Now you can use serilization to open and save the data [assuming you are using base data types]
6. Once it is opened and populated your object, your tree code becomes a simple nested For loop structure:
    pseudo code:
   For each Level1 item
      For each level2 item
         for each levelX item
             

MikeTuersley

  • Guest
Re: Building a treeview with recursion
« Reply #26 on: March 21, 2009, 11:28:02 AM »
Thanks, Tony. You'd be surprised at what I know :kewl:

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: Building a treeview with recursion
« Reply #27 on: March 21, 2009, 02:49:41 PM »
> I have a file (many actually)...


> 1. Use an xml file instead of a text file. Take an existing text file and convert it by adding the xml tags you want:




« Last Edit: March 21, 2009, 02:54:59 PM by Se7en »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MikeTuersley

  • Guest
Re: Building a treeview with recursion
« Reply #28 on: March 21, 2009, 11:33:47 PM »
Ok...so what are you trying to say???

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: Building a treeview with recursion
« Reply #29 on: March 22, 2009, 01:47:53 AM »
1. Whom, or what, will 'add the tags' to the 'many' text files (couldn't--wouldn't--that process put us back to square one)?

2. "Why" are there so many text files (program output, client data, etc.)?

3. Overhead for change in procedure, or conversions?

...

Wouldn't it be easier to model the application for the need not alter the resources for "the" application?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org