Results 1 to 7 of 7

Thread: [RESOLVED] Populating a treeview [2003]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Location
    Lund, Sweden
    Posts
    145

    Resolved [RESOLVED] Populating a treeview [2003]

    Hi

    I would like to populate a treeview with texts from a collection (of objects Metal I've created). The objects contains several data, where some are texts.
    I would like to be able to "sort" this collection in a treeview, where I want to have a couple of root-nodes and under them childnodes. I'll try to explain the structure with this picture:
    Code:
     Metals - Metal manufacturer 01 - Steel 01
                                     - Steel 02
            - Metal Manufacturer 02 - Steel 03
                                     - Iron 01
    This is supposed to be the treeview.
    I could "hardcode" this structure, if I only knew the materials in the collection in advance. But I dont. So I have to iterate through the collection and add Manufacturers (and materials) as I go along.
    I haven't been able to do this as of yet.

  2. #2
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Populating a treeview [2003]

    What have you done so far?

    Dim parentnode as New TreeNode
    Dim childnode as New TreeNode

    parentnode.Text = "asdfasdf"

    Me.TreeView1.Nodes.Add(parentnode)

    childnode.Text = "childasdf"

    parentnode.Nodes.Add(childnode)
    That is how one would programatically add a node to a TreeView, and then add a child node to that node.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

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

    Re: Populating a treeview [2003]

    It's gonna be a pain in the neck trying to parse your text file the way it is right now... I suggest that you save the node's full path to the file instead. That is, instead of writing to the file like this
    Code:
    Metals - Metal manufacturer 01 - Steel 01
                                     - Steel 02
            - Metal Manufacturer 02 - Steel 03
                                     - Iron 01
    You modify your code such that it'll write
    Code:
    Metals
    Metals\Metal manufacturer 01
    Metals\Metal manufacturer 01\Steel 01
    Metals\Metal manufacturer 01\Steel 02
    Metals
    Metals\Metal manufacturer 02
    Metals\Metal Manufacturer 02\Steel 03
    Metals\Metal Manufacturer 02\Iron 01
    And then as you read the text file, for each line you just create a new tree node base on its path.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Location
    Lund, Sweden
    Posts
    145

    Re: Populating a treeview [2003]

    hehe, well. . that wasnt a textfile. I was just trying to describe how I wanted the the treeview to look like

    I have a collection of objects. The objects are of type Metal, a typ e I've made myself. In each object there is a string that holds the material-ID and a string that holds the manufacturer-ID. So I need to iterate through the collection, reading manufacturer from each object and compare that to the current childnodes of the root-node "Metals".
    If I find a childnode that "equals" the string in metalObj.Manufacturer() I want to place a new childnode (materialnode) under the found childnode (\root\manufacturernode\new materialnode). But if I dont find any childnodes that has the same name (i.e manufacturer) I want to make a new childnode to the root (\root\new manufacturernode) and under that new childnode put the materialnode.

    Hope this cleared thing a little bit. I think there are some ways to solve this that I already know of, but I really want to know if there are a simple smart and most of all efficient way of making this happen. Consider that I have to iterate alot and make many new node-arrays if im to use the simple add(node, childs()) method.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Location
    Lund, Sweden
    Posts
    145

    Re: Populating a treeview [2003]

    Hmm, i would really appreciate a little help with this subject. Im completely lost, because I get alot of errors. Such as if I store the manufacturers as strings in an array I only get the last metal dealt with.
    Code:
    Private Sub example()
         Dim length As new Integer
         Dim foundMetal As New Metal
         length = collMetal.count
         Dim stringArray(length) As String
         For i = 1 to length
              foundMetal = collMetal.Item(i)
              stringArray(i-1) = foundMetal.manufacturer()
         Next
    End Sub
    But the problem here is that all "foundMetal" gets cleared whenever the for-loop iterates one more step. This should not be a problem in my opinion, since I just want the string returned by foundMetal.Manufacturer().
    I tried this with a listbox and the following code, which is the same as the one that doesnt work (the array above), and it works perfectly.
    Code:
    Private Sub example()
         Dim length As new Integer
         Dim foundMetal As New Metal
         length = collMetal.count
         Dim stringArray(length) As String
         For i = 1 to length
              foundMetal = collMetal.Item(i)
              lstbFound.Items.Add(foundMetal.Manufacturer)
         Next
    End Sub
    This is quite annoying, since this is a "small" problem of trying to find the solution to the real problem of building a treeview.

  6. #6
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Populating a treeview [2003]

    I am leaving out the declarations you have already shown.

    Dim blnMatchFound as Boolean

    for i = 0 to length - 1
    blnMatchFound = False
    foundMetal = colMetal.Item(i)
    for each pnode as TreeNode in Me.TreeView1.Nodes
    if pnode.Text = foundMetal.Manufacturer then
    blnMatchFound = True
    pnode.Nodes.Add(foundMetal.Material)
    end if
    next
    if Not blnMatchFound then
    Dim NewNode as New TreeNode
    NewNode.Text = foundMetal.Manufacturer
    Me.TreeView1.Nodes.Add(NewNode)
    NewNode.Nodes.Add(foundMetal.Material)
    end if
    next
    I haven't tested this, but the logic is there. For each metal, loop through the nodes in TreeView1 (your tree) looking a manufacturer. If you find one, just add the new node to the previously existing node and set your MatchFound variable to True. Then, after you have looped through all of your toplevel nodes, if MatchFound is False, add the Manufacturer and the new childnode of that manufacturer.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2007
    Location
    Lund, Sweden
    Posts
    145

    Re: Populating a treeview [2003]

    Thank you. Well, I did something resembling your algorithm and got it to work. The last problem was actually just that I had forgotten to add 2 lines of code that stored input from a form to the objects in question.

    Well, I got this to work so im marking this as resolved. Thanks again

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