Go Premium for a chance to win a PS4. Enter to Win

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

Sorting a Treeview

I have a Treeview on my form called vbData.  I thought you could just set the Treeviews sorting property to true and it sorts everything alphabetically.  Is this not true.  I have my code below.

    Set ActiveProject = gVBInstance.ActiveVBProject
   
    tvData.Sorted = True
   
    Set nNode = tvData.Nodes.Add()
    nNode.Text = ActiveProject.Name
   
    For Each cmp In ActiveProject.VBComponents
        Set nNode = tvData.Nodes.Add(1, tvwChild)
        iIndex = nNode.Index
        nNode.Text = cmp.Name
    Next
   
0
midavis
Asked:
midavis
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
in fact, the sorted property of the treeview will only sort the root nodes. You have to set the sorted property for every node that will have children:

.

   Set ActiveProject = gVBInstance.ActiveVBProject
   
   tvData.Sorted = True
   
   Set nNode = tvData.Nodes.Add()
   nNode.Text = ActiveProject.Name
   
   For Each cmp In ActiveProject.VBComponents
       Set nNode = tvData.Nodes.Add(1, tvwChild)
       iIndex = nNode.Index
       nNode.Text = cmp.Name
   Next

   'Sort the children of this node
   nNode.Sorted = true

Cheers
0
 
Richie_SimonettiCommented:
Did you try putting that statement:
tvData.Sorted = True
Just after all noides were added?
0
 
midavisAuthor Commented:
Angel, this seemed to work, sort of.  The last component I add to the tree does not get sorted.  It stays on the top of the node it is supposed to be in.  


    Set ActiveProject = gVBInstance.ActiveVBProject
   
    tvData.Sorted = True
   
    Set nNode = tvData.Nodes.Add()
    nNode.Text = ActiveProject.Name
   
    ' add the main nodes
    For Counter = 1 To 6
        Set nNode = tvData.Nodes.Add(1, tvwChild)
        nNode.Text = mainNodes(Counter)
        nNode.Sorted = True
    Next

This is my updated code.   The mainNodes array holds all the nodes whose parent is the root.  All components I loop through are now added to one of these nodes.  If I have three forms, "frmTest" "frmMain" "frmSimple", in the order they are in the projects, the sorting goes like this.

frmSimple
frmMain
frmTest

Any idea?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
change
nNode.Sorted = True
into
nNode.Parent.Sorted = True

Cheers
     
0
 
midavisAuthor Commented:
The answer you sent me angel was pretty close.  I had to manipulate it a bit, but I said I would give you some more points so here you go.
0

Featured Post

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.

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