Link to home
Start Free TrialLog in
Avatar of midavis
midavis

asked on

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
   
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did you try putting that statement:
tvData.Sorted = True
Just after all noides were added?
Avatar of midavis
midavis

ASKER

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?
change
nNode.Sorted = True
into
nNode.Parent.Sorted = True

Cheers
     
Avatar of midavis

ASKER

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.