c# - Disappearing nodes in TreeView -


after attempting add data that's typed textbox (scanidbox) , clicking addbutton, rootnode seems disappear, , treeview blank. i'm not sure i'm doing wrong here, i'm new windows forms , treeviews. i'm trying add parentnode if item's length 8 , childnode if item's length 9. don't want root node disappear, want items 8 characters long parent node under root node , want other items child nodes of added parent node. how can accomplish this?

public class nodes     {         public treenode rootnode = new treenode();         public treenode parentnode = new treenode();         public treenode childnode = new treenode();     }      public void scan_form_load(object sender, eventargs e)     {         _boxnumberrepository = new boxnumberrepository();          nodes _rootnode = new nodes();         _rootnode.rootnode.text = "scan id";         boxandfilelist.nodes.add(_rootnode.rootnode);          text = "scan form";         acceptbutton = addbutton;         scanidlabel.text = "enter scan id #";         addbutton.text = "add";         removebutton.text = "remove";     }      public void populatetreeview()     {         nodes _rootnode = new nodes();         boxandfilelist.nodes.clear();         scanidbox.text = string.empty;         var items = new list<string>(_boxnumberrepository.getallitems());         foreach (var boxnumber in items)             if (boxnumber.length == 8)             {                 _rootnode.rootnode.nodes.add(boxnumber);             }             else             {                 _rootnode.parentnode.nodes.add(boxnumber);             }     }      public void addbutton_click(object sender, eventargs e)     {         var boxnumber = scanidbox.text.trim();          if (!string.isnullorempty(boxnumber))         {             _boxnumberrepository.additem(boxnumber);             populatetreeview();         }     } 

edit: figured out why node disappearing. clearing nodes in populatetreeview. commented out , stuck around. still can't figure out i'm doing wrong add items. they're still not adding.

edit2: code allowing me add items treeview, still deletes rootnode. i'm not entirely sure need rootnode, think it, i'm not worried it.

public void populatetreeview()     {         boxandfilelist.nodes.clear();         var items = new list<string>(_boxnumberrepository.getallitems());         foreach (var boxnumber in items)             boxandfilelist.nodes.add(boxnumber);         scanidbox.text = string.empty;     } 

i believe need change code to:

public void populatetreeview() {      var items = new list<string>(_boxnumberrepository.getallitems());     foreach (var boxnumber in items)         boxandfilelist.selectednode.nodes.add(boxnumber);     scanidbox.text = string.empty; } 

note adding parent using selectednode , not want clear nodes.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -