Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > treeview


Reply
 
Thread Tools Display Modes
  #1  
Old 11-02-2010, 07:51 PM
nkotbox nkotbox is offline
Newcomer
 
Join Date: Jul 2010
Posts: 11
Question treeview


I am stuck in my project and want to see if anyone can help.

I am building a browser that saves bookmarks to xml but at runtime they are displayed in a treeview. In this view I cannot figure out how to add bookmarks to folders and have them displayed as such in the treeview with all of the bookmarks under each folder.

does anyone have suggestions on creating xml differently or another way to get bookmarks to show correctly in treeview?
Reply With Quote
  #2  
Old 11-03-2010, 08:57 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

It'd help if you could describe what you've tried. Since there's no standard way to display XML content in a TreeView (outside of WPF using a HierarchicalDataTemplate), each solution tends to be unique. That means I can't give you any advice short of writing the whole thing from scratch.

I *think* the question is, "How do I get some nodes to look like a folder?" To do that, you have to provide images in the form of an ImageList to the ImageList property of the TreeView. Then, for the nodes that you want to look like folders, you have to set the TreeNode.ImageIndex property to the appropriate value.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 11-03-2010, 04:45 PM
nkotbox nkotbox is offline
Newcomer
 
Join Date: Jul 2010
Posts: 11
Default

For now I am not too concerned with the images(actually all favorites are displaying folders, but I'll get to those later). I have a form used to enter favorites. The form has a combobox where the user selects the folder the fav should be added under. A button to add a new folder and an add button to confirm all is chosen and ready to add favorite(fav)

Code:
dim combo as string=combobox1.selecteditem
dim fav as string=webpagetitle (coorelating webpage title and actual link is taken care of elsewhere)
Dim tn() As TreeNode = Form1.FavTreeView.Nodes.Find(combo, True)
            Dim i As Integer = 0

            For i = 0 To tn.Length
                Form1.FavTreeView.SelectedNode = tn(i)
                Form1.FavTreeView.SelectedNode.Nodes.Add(fav)
            Next i
Thought this would work to add links correctly to treeview but it doesn't.
also once added to XML I am unsure how to add combo as parent and link as child in XML sheet as there will always be new combo(folder names) added.
Reply With Quote
  #4  
Old 11-03-2010, 06:35 PM
nkotbox nkotbox is offline
Newcomer
 
Join Date: Jul 2010
Posts: 11
Default

ok, have figured out my treeview display issue and why my above code was not working coirrectly. Rookie mistake of not naming combo before adding as node.
combo.name=combo then add as node to treeview makes it lot easier to find

anyway I have tried several variants of the code below. When I write the favorites to XMl I want to have the folders as elements and the favorites of each folders as childs of the corresponding element. Can someone suggest how to iterate thru the folders and add the favorites as child elements to its corresponding folder?

Code:
Public _favs As New List(Of Favorites)
Dim a1 As New Favorites (favorites is a class where I save the webpage,title and folder it belongs in)
  For Each a1 As Favorites In _favs
            Dim FavoritesElement As XmlElement = doc.CreateElement("Favorite")
            Dim a_webpage As XmlElement = MakeElement(doc, "WebPage", a1.webpage)
            Dim a_webpagetitle As XmlElement = MakeElement(doc, "WebPageTitle", a1.webpagetitle)
            Dim a_webpageIndex As XmlElement = MakeElement(doc, "WebPageIndex", a1.webpageIndex)
            Dim a_webpageFolder As XmlElement = MakeElement(doc, "WebPageFolder", a1.webpageFolder)
            With FavoritesElement
                .AppendChild(a_webpage)
                .AppendChild(a_webpagetitle)
                .AppendChild(a_webpageIndex)
                .AppendChild(a_webpageFolder)
            End With
            root.AppendChild(FavoritesElement)
        Next
Reply With Quote
  #5  
Old 11-04-2010, 12:44 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

Well, I could waste your time and ask for enough information to make a perfect example or I could just implement something and let you figure out how to apply it. I think the latter will work.

I was going to just post code in the thread but it got bigger than I thought it would; I've attached the VS 2010 project. I'm leaning heavily on some stuff that I can't remember if VS 2008 supported, so you'll have to speak up if it doesn't work.

I think the best thing to do is separate the notions of saving the bookmarks and displaying them. By that I mean it'd be smart to have some classes in between that aren't TreeNodes. If you do this, it can enable some nifty tricks (one of which didn't even work due to what I consider a bug) that might help. The in-between classes are Folder and Bookmark. Their APIs leave a lot to be desired, but they work for the example.

The XML is saved and loaded in the BookmarkXmlFile class, mostly using LINQ to XML. I feel like that part's boring, so I'll explain how to use it more than how it works. If you want to load the file, you call the shared Load() method. If you need to save some bookmarks, you build a hierarchy of Folder objects that contain Bookmarks, put them in an array, set the Folders property, then call Save(). It's a pretty bad API, but again it works for the example.

That brings us to the form. It's pretty spartan because I was in a hurry. Loading bookmarks goes through a process. First, the file is loaded via a BookmarkXmlFile instance. Then, each Folder object is converted to a TreeNode and added to the TreeView. I stash the folder in the tag for reasons that will become clear later. Part of converting a Folder to a TreeNode is creating a child node for each Bookmark it contains. Again, the Bookmark gets stashed in the Tag property.

The "Bookmark" button adds a bookmark. In this example, bookmarks can only be added if a folder is selected. I check this by looking at the node's Tag property; if it's not a Folder nothing is done. If the selected node is a folder, a Bookmark object is created along with a TreeNode for it. The TreeNode's tag is set and the node is added to the bookmark.

The "Folder" button adds a folder to the nodes. It generates a Folder object and a TreeNode, tags the node, and adds it to the tree.

The "Delete" button doesn't care whether it's pointed at a folder or bookmark; it just deletes the node that's selected.

The "Save" button converts the nodes into a hierarchy of Folder and Bookmark objects, then uses a BookmarkXmlFile to save the bookmarks.

There's a lot missing here; my solution doesn't let you add bookmarks unless they're in a folder. It doesn't have anything to get the URL from a selected bookmark (though the Tag property is convenient for this). But this is a functional example that might get you farther faster than more back and forth to discuss particulars could.
Attached Files
File Type: zip Bookmarks.zip (14.0 KB, 2 views)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 11-05-2010, 06:10 PM
nkotbox nkotbox is offline
Newcomer
 
Join Date: Jul 2010
Posts: 11
Default

Ok, thanks everyone, I have got it working correctly now. Rather than changing the way I made the XML i changed the way I displayed the nodes.
I do have a class that stores folder,url & url title for each bookmark. I then iterate thru the xml 1 time to store all folders as nodes, while checking for dupes and not displaying those. Then I go thru the xml a second time, this time using the folder as a call for selected node and add the url title as the child of the selected node.

For some reason though the folders and bookmarks have the same icon. Is there a way to change icons for parent and child nodes in treeview?
Reply With Quote
  #7  
Old 11-06-2010, 08:42 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

Set their ImageIndex property to different values.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #8  
Old 11-06-2010, 09:19 AM
nkotbox nkotbox is offline
Newcomer
 
Join Date: Jul 2010
Posts: 11
Default

Thanks, I was using selectedimageindex which was the wrong choice.

marching fwd
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->