FAQ: How to add/sort items Drop Buttons items in PowerToolbar

One of my customers contacted me couple of days ago to ask if it was possible to sort items in a drop button, when the items are created at runtime. Since the items are stored in a queue I figured it would be fairly easy.

 Toolbar1.DeleteItems(ID1_Name)

 Toolbar1.AddDropItem(ID1_Name,'Arnor','ok_16.ico',1)
 Toolbar1.AddDropItem(ID1_Name,'Robert','ok_16.ico',2)
 Toolbar1.AddDropItem(ID1_Name,'Monica','ok_16.ico',3)
 Toolbar1.AddDropItem(ID1_Name,'Ana Maria','ok_16.ico',4)
 Toolbar1.AddDropItem(ID1_Name,'Ivan','ok_16.ico',5)

 Sort(Toolbar1.BandQ.ControlQ.pDropButton.ItemQ,|
      Toolbar1.BandQ.ControlQ.pDropButton.ItemQ.szTxt)
 !! Reset the ItemIDs after sorting the queue.
 Loop I# = 1 To Records(Toolbar1.BandQ.ControlQ.pDropButton.ItemQ)
   Get(Toolbar1.BandQ.ControlQ.pDropButton.ItemQ,I#)
   Toolbar1.BandQ.ControlQ.pDropButton.ItemQ.ItemID = I#
   Put(Toolbar1.BandQ.ControlQ.pDropButton.ItemQ)
 End
 Display()

Note that this will not work properly if you have separators in the drop items. If you want to do that, I would suggest to add the item text entries to a queue and sort them before you add them to the drop button. Something like this might work:

AddDropButtonItems         ROUTINE
 Data
Q   Queue
T     CString(256) !! Item text
SF    Byte         !! Separator Follows
    End
 Code
 Q.T = 'Arnor' ;     Q.SF = False; Add(Q)
 Q.T = 'Robert' ;    Q.SF = True;  Add(Q)
 Q.T = 'Monica' ;    Q.SF = False; Add(Q)
 Q.T = 'Ana Maria' ; Q.SF = False; Add(Q)
 Q.T = 'Ivan' ;      Q.SF = False; Add(Q)
 Sort(Q,Q.T)
 Toolbar1.DeleteItems(ID1_Name)
 Loop X# = 1 To Records(Q)
   Get(Q,X#)
   Toolbar1.AddDropItem(ID1_Name,Q.T,'ok_16.ico',1)
   If Q.SF = True !! Add Separator
     Toolbar1.AddDropItem(ID1_Name,'-','ok_16.ico',1)
   End
 End

 Display()

This should be able to handle sorted list with separators. I'm planning to add a method to sort the items alphabetically and I think I can make it handle the separators correctly too. The method I describe here is a bit more flexible as it gives you full control over how the items are arranged.

Arnor Baldvinsson

Leave a Reply