Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. treenodecollection nodes not working as expected with created class [SOLVED]

treenodecollection nodes not working as expected with created class [SOLVED]

Scheduled Pinned Locked Moved C#
csharpdatabaselinqdata-structurestutorial
10 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tonyonlinux
    wrote on last edited by
    #1

    main forum:

    private void Filter_combo_SelectedIndexChanged(object sender, EventArgs e)
    {
    /* Populate the tree based on whatever the user selects to filter by
    * for example if they click on keywords then only show the keywords
    */

            string filter = Filter\_combo.Text;
            
          
    
    
    
            using (var db = new mombooksDataContext(Properties.Settings.Default.BooksConnectionString2))
            {
                NodeBuilder.BuildNodes(treeView1.Nodes,db.Authors, db.Books, filter);
                treeView1.Nodes.Clear();//get rid of t his after we're done. 
    
            }
        }
    

    class

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using momdb;

    namespace Utilties
    {
    public sealed class NodeBuilder
    {
    public static void BuildNodes(TreeNodeCollection nodes,IEnumerable<Author>authors, IEnumerable<Book> books, string filter)
    {

            if (filter == "Keywords" || filter == "Title")
            {
                books.ToList().ForEach(m =>
                    {
                        TreeNode node = new TreeNode(filter);
                        node.Tag = m;
                        nodes.Add(node);
                    });
            }
           if (filter =="Author")
           {
               authors.ToList().ForEach(m =>
                {
                TreeNode node =new TreeNode(filter); 
                node.Tag=m;
                nodes.Add(node);
                 }); 
           }
        }
    }
    

    whenever i do this I never get any results to my treeview and I can't really comprehend why not. I'm passing it the nodes and I would think it would populate the tree from those nodes :(

    modified on Friday, February 19, 2010 11:00 PM

    D 1 Reply Last reply
    0
    • T tonyonlinux

      main forum:

      private void Filter_combo_SelectedIndexChanged(object sender, EventArgs e)
      {
      /* Populate the tree based on whatever the user selects to filter by
      * for example if they click on keywords then only show the keywords
      */

              string filter = Filter\_combo.Text;
              
            
      
      
      
              using (var db = new mombooksDataContext(Properties.Settings.Default.BooksConnectionString2))
              {
                  NodeBuilder.BuildNodes(treeView1.Nodes,db.Authors, db.Books, filter);
                  treeView1.Nodes.Clear();//get rid of t his after we're done. 
      
              }
          }
      

      class

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      using momdb;

      namespace Utilties
      {
      public sealed class NodeBuilder
      {
      public static void BuildNodes(TreeNodeCollection nodes,IEnumerable<Author>authors, IEnumerable<Book> books, string filter)
      {

              if (filter == "Keywords" || filter == "Title")
              {
                  books.ToList().ForEach(m =>
                      {
                          TreeNode node = new TreeNode(filter);
                          node.Tag = m;
                          nodes.Add(node);
                      });
              }
             if (filter =="Author")
             {
                 authors.ToList().ForEach(m =>
                  {
                  TreeNode node =new TreeNode(filter); 
                  node.Tag=m;
                  nodes.Add(node);
                   }); 
             }
          }
      }
      

      whenever i do this I never get any results to my treeview and I can't really comprehend why not. I'm passing it the nodes and I would think it would populate the tree from those nodes :(

      modified on Friday, February 19, 2010 11:00 PM

      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      try replacing

      NodeBuilder.BuildNodes(treeView1.Nodes,db.Authors, db.Books, filter); treeView1.Nodes.Clear();//get rid of t his after we're done.

      with:

      TreeNodeCollection[] nodes = new TreeNodeCollection[treeView1.Nodes.Count];
      treeView1.Nodes.CopyTo(nodes, 0);
      treeView1.Nodes.Clear();//get rid of this BEFORE we're done.
      treeView1.Nodes.AddRange(NodeBuilder.BuildNodes(nodes, db.Authors, db.Books, filter));

      and modify your BuildNodes method to return an array of TreeNode.

      T 2 Replies Last reply
      0
      • D Dan Mos

        try replacing

        NodeBuilder.BuildNodes(treeView1.Nodes,db.Authors, db.Books, filter); treeView1.Nodes.Clear();//get rid of t his after we're done.

        with:

        TreeNodeCollection[] nodes = new TreeNodeCollection[treeView1.Nodes.Count];
        treeView1.Nodes.CopyTo(nodes, 0);
        treeView1.Nodes.Clear();//get rid of this BEFORE we're done.
        treeView1.Nodes.AddRange(NodeBuilder.BuildNodes(nodes, db.Authors, db.Books, filter));

        and modify your BuildNodes method to return an array of TreeNode.

        T Offline
        T Offline
        tonyonlinux
        wrote on last edited by
        #3

        okay i been battling this for hours. I tried taking and returning a treenode[] but i keep getting can't convert type treenode to treenode[]? any chance of a little more insight on this please ?

        modified on Friday, February 19, 2010 11:00 PM

        1 Reply Last reply
        0
        • D Dan Mos

          try replacing

          NodeBuilder.BuildNodes(treeView1.Nodes,db.Authors, db.Books, filter); treeView1.Nodes.Clear();//get rid of t his after we're done.

          with:

          TreeNodeCollection[] nodes = new TreeNodeCollection[treeView1.Nodes.Count];
          treeView1.Nodes.CopyTo(nodes, 0);
          treeView1.Nodes.Clear();//get rid of this BEFORE we're done.
          treeView1.Nodes.AddRange(NodeBuilder.BuildNodes(nodes, db.Authors, db.Books, filter));

          and modify your BuildNodes method to return an array of TreeNode.

          T Offline
          T Offline
          tonyonlinux
          wrote on last edited by
          #4

          here is what I have

          private void Filter_combo_SelectedIndexChanged(object sender, EventArgs e)
          {
          /* Populate the tree based on whatever the user selects to filter by
          * for example if they click on keywords then only show the keywords
          */

                  string filter = Filter\_combo.Text;
                  
                
          
          
          
                  using (var db = new mombooksDataContext(Properties.Settings.Default.BooksConnectionString2))
                  {
                      
                      TreeNodeCollection\[\] nodes = new TreeNodeCollection\[treeView1.Nodes.Count\];
                      treeView1.Nodes.CopyTo(nodes, 0);
                      treeView1.Nodes.Clear();//get rid of this BEFORE we're done. 
                      treeView1.Nodes.AddRange(NodeBuilder.BuildNodes(nodes, db.Authors, db.Books, filter));
                      
                  }
              }
          

          class

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Windows.Forms;
          using momdb;

          namespace Utilties
          {
          public sealed class NodeBuilder
          {
          // public static void BuildNodes(TreeNodeCollection nodes,IEnumerable<Author>authors, IEnumerable<Book> books, string filter)
          public static TreeNode[] BuildNodes(TreeNodeCollection nodes,IEnumerable<Author>authors, IEnumerable<Book> books, string filter)
          {
          TreeNode[] holder;

                  if (filter == "Keywords" || filter == "Title")
                  {
                      
                      filter = string.Format("m.{0}", filter);
                      books.ToList().ForEach(m =>
                          {
                              TreeNode node = new TreeNode(filter);
                              node.Tag = m;
                              nodes.Add(node);
                              
                          });
                     
                  }
                 if (filter =="Author")
                 {
                     authors.ToList().ForEach(m =>
                      {
                      TreeNode node =new TreeNode(filter); 
                      node.Tag=m;
                      nodes.Add(node);
                       }); 
                 }
                 return holder = new TreeNode\[nodes.Count\];
              }
            
          }
          

          }

          D 1 Reply Last reply
          0
          • T tonyonlinux

            here is what I have

            private void Filter_combo_SelectedIndexChanged(object sender, EventArgs e)
            {
            /* Populate the tree based on whatever the user selects to filter by
            * for example if they click on keywords then only show the keywords
            */

                    string filter = Filter\_combo.Text;
                    
                  
            
            
            
                    using (var db = new mombooksDataContext(Properties.Settings.Default.BooksConnectionString2))
                    {
                        
                        TreeNodeCollection\[\] nodes = new TreeNodeCollection\[treeView1.Nodes.Count\];
                        treeView1.Nodes.CopyTo(nodes, 0);
                        treeView1.Nodes.Clear();//get rid of this BEFORE we're done. 
                        treeView1.Nodes.AddRange(NodeBuilder.BuildNodes(nodes, db.Authors, db.Books, filter));
                        
                    }
                }
            

            class

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Windows.Forms;
            using momdb;

            namespace Utilties
            {
            public sealed class NodeBuilder
            {
            // public static void BuildNodes(TreeNodeCollection nodes,IEnumerable<Author>authors, IEnumerable<Book> books, string filter)
            public static TreeNode[] BuildNodes(TreeNodeCollection nodes,IEnumerable<Author>authors, IEnumerable<Book> books, string filter)
            {
            TreeNode[] holder;

                    if (filter == "Keywords" || filter == "Title")
                    {
                        
                        filter = string.Format("m.{0}", filter);
                        books.ToList().ForEach(m =>
                            {
                                TreeNode node = new TreeNode(filter);
                                node.Tag = m;
                                nodes.Add(node);
                                
                            });
                       
                    }
                   if (filter =="Author")
                   {
                       authors.ToList().ForEach(m =>
                        {
                        TreeNode node =new TreeNode(filter); 
                        node.Tag=m;
                        nodes.Add(node);
                         }); 
                   }
                   return holder = new TreeNode\[nodes.Count\];
                }
              
            }
            

            }

            D Offline
            D Offline
            Dan Mos
            wrote on last edited by
            #5

            tonyonlinux wrote:

            return holder = new TreeNode[nodes.Count];

            this line returns an empty array of Lenght nodes.Count. use something like

            holder = new TreeNode[nodes.Count];
            holder.AddRange(nodes);
            return holder;

            T 1 Reply Last reply
            0
            • D Dan Mos

              tonyonlinux wrote:

              return holder = new TreeNode[nodes.Count];

              this line returns an empty array of Lenght nodes.Count. use something like

              holder = new TreeNode[nodes.Count];
              holder.AddRange(nodes);
              return holder;

              T Offline
              T Offline
              tonyonlinux
              wrote on last edited by
              #6

              there doesn't appear to be an addrange associate with holder. would it perhaps be another method I have to call ?

              D 1 Reply Last reply
              0
              • T tonyonlinux

                there doesn't appear to be an addrange associate with holder. would it perhaps be another method I have to call ?

                D Offline
                D Offline
                Dan Mos
                wrote on last edited by
                #7

                yep. just use a for(int i=0;i

                T 1 Reply Last reply
                0
                • D Dan Mos

                  yep. just use a for(int i=0;i

                  T Offline
                  T Offline
                  tonyonlinux
                  wrote on last edited by
                  #8

                  here is what I've come up with so far. 208 nodes get put into the array i checked that in the debugger but i get an exception each time it finds a instance where the node is the same. in example says can not add or insert the item 'tbone,eater' in more than one place. here is what i'm using in my main form

                  private void Filter_combo_SelectedIndexChanged(object sender, EventArgs e)
                  {
                  /* Populate the tree based on whatever the user selects to filter by
                  * for example if they click on keywords then only show the keywords
                  */

                          string filter = Filter\_combo.Text;
                          
                        
                  
                  
                  
                          using (var db = new mombooksDataContext(Properties.Settings.Default.BooksConnectionString2))
                          {
                              
                              TreeNodeCollection\[\] nodes = new TreeNodeCollection\[treeView1.Nodes.Count\];
                              treeView1.Nodes.CopyTo(nodes, 0);
                              treeView1.Nodes.Clear();//get rid of this BEFORE we're done. 
                              treeView1.Nodes.AddRange(NodeBuilder.BuildNodes(treeView1.Nodes, db.Authors, db.Books, filter));
                              
                          }
                      }
                  

                  then i'm calling the modified class

                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;
                  using System.Windows.Forms;
                  using momdb;

                  namespace Utilties
                  {
                  public sealed class NodeBuilder
                  {
                  // public static void BuildNodes(TreeNodeCollection nodes,IEnumerable<Author>authors, IEnumerable<Book> books, string filter)
                  public static TreeNode[] BuildNodes(TreeNodeCollection nodes,IEnumerable<Author>authors,IEnumerable<Book>books,string filter)
                  {
                  TreeNode node = new TreeNode();

                          if (filter == "Keywords")
                            {
                                node = nodes.Add("Keywords");
                              
                                books.ToList().ForEach(m =>
                                  {
                                      node.Nodes.Add(m.Keywords);
                                      node.Tag = filter;
                                      
                                      
                                  });
                             
                          }
                          if (filter == "Title")
                          {
                              node = nodes.Add("Title");
                              books.ToList().ForEach(m =>
                                  {
                                      node.Nodes.Add(m.Title);
                                      node.Tag = filter;
                                  });
                          }
                         if (filter =="Author")
                  
                  D 1 Reply Last reply
                  0
                  • T tonyonlinux

                    here is what I've come up with so far. 208 nodes get put into the array i checked that in the debugger but i get an exception each time it finds a instance where the node is the same. in example says can not add or insert the item 'tbone,eater' in more than one place. here is what i'm using in my main form

                    private void Filter_combo_SelectedIndexChanged(object sender, EventArgs e)
                    {
                    /* Populate the tree based on whatever the user selects to filter by
                    * for example if they click on keywords then only show the keywords
                    */

                            string filter = Filter\_combo.Text;
                            
                          
                    
                    
                    
                            using (var db = new mombooksDataContext(Properties.Settings.Default.BooksConnectionString2))
                            {
                                
                                TreeNodeCollection\[\] nodes = new TreeNodeCollection\[treeView1.Nodes.Count\];
                                treeView1.Nodes.CopyTo(nodes, 0);
                                treeView1.Nodes.Clear();//get rid of this BEFORE we're done. 
                                treeView1.Nodes.AddRange(NodeBuilder.BuildNodes(treeView1.Nodes, db.Authors, db.Books, filter));
                                
                            }
                        }
                    

                    then i'm calling the modified class

                    using System;
                    using System.Collections.Generic;
                    using System.Linq;
                    using System.Text;
                    using System.Windows.Forms;
                    using momdb;

                    namespace Utilties
                    {
                    public sealed class NodeBuilder
                    {
                    // public static void BuildNodes(TreeNodeCollection nodes,IEnumerable<Author>authors, IEnumerable<Book> books, string filter)
                    public static TreeNode[] BuildNodes(TreeNodeCollection nodes,IEnumerable<Author>authors,IEnumerable<Book>books,string filter)
                    {
                    TreeNode node = new TreeNode();

                            if (filter == "Keywords")
                              {
                                  node = nodes.Add("Keywords");
                                
                                  books.ToList().ForEach(m =>
                                    {
                                        node.Nodes.Add(m.Keywords);
                                        node.Tag = filter;
                                        
                                        
                                    });
                               
                            }
                            if (filter == "Title")
                            {
                                node = nodes.Add("Title");
                                books.ToList().ForEach(m =>
                                    {
                                        node.Nodes.Add(m.Title);
                                        node.Tag = filter;
                                    });
                            }
                           if (filter =="Author")
                    
                    D Offline
                    D Offline
                    Dan Mos
                    wrote on last edited by
                    #9

                    I don't know exactly and right know I don't have the time to try it and see what is wrong. I mean the message is clear => something with duplicate items. You could try to return the Tree nodes collection and of course edit the method to return a Collection not array. See if it helps

                    T 1 Reply Last reply
                    0
                    • D Dan Mos

                      I don't know exactly and right know I don't have the time to try it and see what is wrong. I mean the message is clear => something with duplicate items. You could try to return the Tree nodes collection and of course edit the method to return a Collection not array. See if it helps

                      T Offline
                      T Offline
                      tonyonlinux
                      wrote on last edited by
                      #10

                      I figured it out thanks for your time...

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups