Results 1 to 16 of 16

Thread: Dynamic TreeView issue - need help

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Dynamic TreeView issue - need help

    I am trying to dynamically build a TreeView. I want to be able to expand a top level node & have any child nodes be added on the fly. The problem I'm running into is that I can't get the "+" symbol to appear if there are no subnodes to begin with. I could probably add a "dummy" subnode, then when the parent node is expanded I could delete the dummy node & add my real nodes, but that sounds cheesy. Can someone please explain the proper way to do this or point me to some code? Thanks...

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Dynamic TreeView issue - need help

    Hmm... I think the simple answer is there isn't any way other than your "cheesy" way (which I don't think is cheesy by the way - it is used quite commonly).

    The "+" is there to signify the presence of child nodes. If there are no child nodes there is no "+", simple as that.

    The only other thing you may be able to do is make the treeview "owner-draw" (make the treeview's DrawMode = OwnerDrawAll) and you would be responsible for painting the content of the treeview yourself, which is a lot more hassle than doing it by creating dummy nodes.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dynamic TreeView issue - need help

    This is something I've been intending to investigate for a while because I'd like to be able to do the same thing. I'm guessing that it's possible to inherit the TreeNode and/or TreeView classes and make this happen, although it might take some messing with unmanaged code. I'll schedule it for investigation this weekend and let you know how I get on.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Dynamic TreeView issue - need help

    Please do let us know what you find out. My "cheesy" way does work, but I'm sure anything you come up with will be far more elegant.

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Dynamic TreeView issue - need help

    I have to say I have always used the way you described. I just add a node that says "Loading..." or something like that so that if somehow the user does see it for a second before the proper nodes are added then thats all they see until the dynamic nodes are added. I dont really see anything wrong with this method but I would be interested to see if it could be done more 'properly'
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Dynamic TreeView issue - need help

    You don't even need to see a temporary node at all if you populate your branch during the node BeforeExpand event.

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Dynamic TreeView issue - need help

    Ah I see, never tried that but I will bear it in mind if I ever need to do that again

    PS it took me about 30 seconds to make this work the way you want to in WPF. Its the future, I'm telling you!
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Dynamic TreeView issue - need help

    Quote Originally Posted by chris128 View Post

    PS it took me about 30 seconds to make this work the way you want to in WPF. Its the future, I'm telling you!
    I think it's a bit too early to say that right now. WPF could take off like a rocket or it could quitely die off in a few years just like many other technology innovations.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Dynamic TreeView issue - need help

    You dont think the fact that the new version of the primary application that Windows developers use (Visual Studio) is made in WPF shows that it may be slightly more serious than "just another technology" ? I'm certainly not saying it is perfect and it needs to mature quite a lot still but I was just commenting on how easy it makes things like this...
    Anyway I guess this is not really a discussion to be had in this thread
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Dynamic TreeView issue - need help

    How would you want the TreeView to react to the following situation:

    You have a TreeNode that has no children and you select it, the [+] box goes away. Then you navigate away from that node (by selecting a different one). Should the Node redraw that [+] OR should it stay cleared? Because potentially nodes could get dynamically added to that node after you select a different one.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Dynamic TreeView issue - need help

    I think it should stay cleared. That's how I made mine behave.

  12. #12
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Dynamic TreeView issue - need help

    Oh I thought the idea was that if the node did not have any children then it wouldnt get the plus symbol. I think it would look pretty naff having a plus symbol that when the user clicks it just disappears :S
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Dynamic TreeView issue - need help

    In my case, there's no way to know ahead of time if a node will have children or not, so I just always put the "+" there. If the user expands it & there are no child nodes, then it clears.

  14. #14
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Dynamic TreeView issue - need help

    Oh right, would have taken even less time in WPF if I had known that (sorry stanav )
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Dynamic TreeView issue - need help

    JMC ... just wondering if you had a chance to investigate this...

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dynamic TreeView issue - need help

    Quote Originally Posted by nbrege View Post
    JMC ... just wondering if you had a chance to investigate this...
    I had a look through the TreeView code in Reflector and didn't see anything that appeared useful. I'll take a closer look at the Win32 control some time soon.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width