[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

Add properties and numbering to Treeview

HI,

I'm using a treeview control which is being feed by a database.  The tag property is already used to define relationship among nodes (Node's Unique Identifier) so it's of no use.  I would like to:

A- Be able to show numbers in front of every nodes, dynamically generated upon their rank in treeview ex:

1.0 Parent Node
  1.1  Child node
  1.2  Child Node
2.0 Parent Node .....

B- Make sure nodes are sorted even if I drag&drop a node elsewhere in the treeview(then entire numbering has to be redefined)

C- I would like to save the "number" in the database as well as to add a description of the node in the database. ie: is it possible to add  properties to the treeview (like a description and the number)?

Thanks
0
vicmart69
Asked:
vicmart69
  • 4
  • 4
  • 2
2 Solutions
 
eekjCommented:
It will get messy.. I think the Tag is the best place. You know you can tag any object to a node so why not have the tag define the relationship AND store the node's unique id AND the number AND the text?
0
 
arif_eqbalCommented:
I just wrote a simple function that will do exactly what you want
Load the treeview as you will normally, after you have loaded the tree call this function as

 IndexTree("", TreeView1.Nodes)

This function works on an already loaded treeview and modifies the Text of each node to also display and Index number. Also since it works on an already loaded treeview in case you want to re-number it say after an Item has been Dragged & dropped, call the function again, that's it  

    Private Sub IndexTree(ByVal Index As String, ByVal cNodes As TreeNodeCollection)
        Dim Nd As TreeNode
        Dim ChildCount As Integer = 1

        For Each Nd In cNodes
            If Nd.Text.IndexOf(" - ") > 0 Then
                Nd.Text = Nd.Text.Substring(Nd.Text.IndexOf(" - ") + 3)
            End If
            Nd.Text = Index & CStr(ChildCount) & " - " & Nd.Text
            If Nd.GetNodeCount(True) > 0 Then
                IndexTree(Index & CStr(ChildCount) & ".", Nd.Nodes)
            End If
            ChildCount += 1
        Next
    End Sub


I have a bit generalised the function so here is how it works
You need to pass an Initial Index (at start that will be empty string) and a collection of nodes to it. It iteratively checks for each child node and numbers it. It uses recursive calls to the same function and will work for any level of nesting.


0
 
arif_eqbalCommented:
I hope you can find out one limitation of the code
It will fail if you have a Dash " - " in the actual text of the node
I hope that will not be a big problem though

what you can do is use any other character in place of dash which your nodes will not have, better still you can pass the cahracter as an argument to the function so that it uses that particular cahracter it will help modularize the code, so that it can be used for any tree
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
eekjCommented:
>>is it possible to add  properties to the treeview (like a description and the number)?
Alternatively, yes you can extend any object and add whatever you like.

Public Class SpecialTreeView
    Inherits System.Windows.Forms.TreeNode

    Dim _descript As String
    Dim _id As String

    Public Property ID() As String
        Get
            Return _id
        End Get
        Set(ByVal Value As String)
            _id = Value
        End Set
    End Property


    Public Property Descript() As String
        Get
            Return _descript
        End Get
        Set(ByVal Value As String)
            _descript = Value
        End Set
    End Property

End Class
0
 
arif_eqbalCommented:
Well eekj

I think he wants a Description property for each node of the treeview
Not for the TreeView itself

0
 
eekjCommented:
Whoops, thats a typo... heh. Oh well.

Public Class SpecialTreeNode ' -- not treeview
    Inherits System.Windows.Forms.TreeNode
0
 
vicmart69Author Commented:
eekj:

In your first comment, you say that it would get messy..  do you mean that I really shouldn't add properties to the treeview and use the TAG?  Or should I use the code to add properties that you described?

If I'd better use the tag, how could I handle the format then?


arif_eqbal: What do you think?

Thanks,

MA
0
 
eekjCommented:
Sorry for being confusing. I think you should use the Tag to store an object that has everything you need for that node.
The tag could store who the node's parent node is, and when you reset the text on that node after moving it, a method in the tag could update the TreeNode and set it's text to the parent node's index number plus its own index number plus the node's decription. This way you dont have to worry about finding the index of a character or whatever, its more normalised in a sense.

Alternatively by inheriting from TreeNode you could add your own properties because your new TreeNode class would have everything a normal TreeNode has and also your added properties. It would behave like a TreeNode and fit anywhere a normal TreeNode would fit as well as doing the extra stuff you want it to do.

Just my 2c.




0
 
arif_eqbalCommented:
If you can use the tag its better, go for new property if tag is already used for something else...

0
 
vicmart69Author Commented:
Thank you very much guys for your help.  the numbering function works like a charm and the property stuff is really useful to me.

MA
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now