Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > ActiveX COM > Command Bars
  New Posts New Posts RSS Feed - Iterating all controls
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Iterating all controls

 Post Reply Post Reply
Author
Message
Fabian View Drop Down
Senior Member
Senior Member
Avatar

Joined: 22 November 2004
Location: Switzerland
Status: Offline
Points: 336
Post Options Post Options   Thanks (0) Thanks(0)   Quote Fabian Quote  Post ReplyReply Direct Link To This Post Topic: Iterating all controls
    Posted: 09 September 2010 at 1:51am
Hi all,
 
I am searching for a simple way to enumerate all controls of all commandbars AND menues. It seems no problem to accomplish this task by simply:
 
For Each Toolbar in CommandBars
  For Each Ctrl in Toolbar.Controls
...
 
But with this code I never reach the controls in my menues, only controls placed on 'real' toolbars.
 
 
 
Any idea, help?
Thanks in advance
Fabian
Product: Xtreme SuitePro (ActiveX) version 16.2.3

Platform: Windows 7 (32bit)

Language: Visual Basic 6.0 / SP6
Back to Top
jpbro View Drop Down
Senior Member
Senior Member
Avatar

Joined: 12 January 2007
Status: Offline
Points: 1354
Post Options Post Options   Thanks (0) Thanks(0)   Quote jpbro Quote  Post ReplyReply Direct Link To This Post Posted: 09 September 2010 at 10:10am
You need to recursively enumerate child CommandBars of the CommandBarControls that you find. I haven't completely tested this code, but it should at least point you in the right direction:


Private Sub EnumAll()
   Dim lo_CommandBar As CommandBar
   Dim lo_Control As CommandBarControl
   Dim i As Long
  
   ' Enumerate top-level commandbars and their controls
   For Each lo_CommandBar In Me.CommandBars1
      Debug.Print lo_CommandBar.Title
     
      ' Enumerate this commandbar's controls
      ' EnumControls is a recursive function that will also enumerate the child controls controls,
      ' and the child controls of that's child's controls, etc....
      EnumControls lo_CommandBar
   Next lo_CommandBar

   Exit Sub
End Sub


Private Sub EnumControls(po_CommandBar As XtremeCommandBars.CommandBar, Optional ByVal p_IndentLevel As Long = 1)
   ' p_IndentLevel is used for printing to the debug window, and not needed in production code
   '               if you don't need to display the control hierarchy in a formatted/indented manner
  
   Dim lo_Control As XtremeCommandBars.CommandBarControl
  
   For Each lo_Control In po_CommandBar.Controls
      ' Print the control name at an indentation level that represents it's position in the controls tree
      Debug.Print String$(p_IndentLevel, vbTab); lo_Control.Caption
     
      ' Check to see if this control has a CommandBar child, if so it could have child controls
      ' that need enumerating
     
      ' Don't test for CommandBar in a CommandBarControlCustom object, because it will raise an error
      ' There may be other CommandBar control types that raise errors, you will have to test for this
      If Not (TypeOf lo_Control Is XtremeCommandBars.CommandBarControlCustom) Then
         ' This is not a CommandBarControlCustom object, so let's see if it has a CommandBar
        
         If Not (lo_Control.CommandBar Is Nothing) Then
            ' This control has a CommandBar, so recursively call this function to enumerate it's controls
           
            EnumControls lo_Control.CommandBar, p_IndentLevel + 1
         End If
      End If
   Next lo_Control
End Sub


Call EnumAll (changing the reference of Me.CommandBars1 to the name of your CommandBars control) and you should get a list of all controls printed to the debug window.


Product: Xtreme SuitePro (ActiveX) version 16.2.6
Platform: Windows XP - SP3

Language: Visual Basic 6.0 SP6

Back to Top
Fabian View Drop Down
Senior Member
Senior Member
Avatar

Joined: 22 November 2004
Location: Switzerland
Status: Offline
Points: 336
Post Options Post Options   Thanks (0) Thanks(0)   Quote Fabian Quote  Post ReplyReply Direct Link To This Post Posted: 09 September 2010 at 10:24am
Hi jpbro,
 
Thanks a lot. I will check your clean code
 
Regards
Fabian
Product: Xtreme SuitePro (ActiveX) version 16.2.3

Platform: Windows 7 (32bit)

Language: Visual Basic 6.0 / SP6
Back to Top
jpbro View Drop Down
Senior Member
Senior Member
Avatar

Joined: 12 January 2007
Status: Offline
Points: 1354
Post Options Post Options   Thanks (0) Thanks(0)   Quote jpbro Quote  Post ReplyReply Direct Link To This Post Posted: 09 September 2010 at 10:35am
There was a small bug in my code - lo_Toolbar in EnumAll should have been lo_CommandBar. I've update the code above
Product: Xtreme SuitePro (ActiveX) version 16.2.6
Platform: Windows XP - SP3

Language: Visual Basic 6.0 SP6

Back to Top
Fabian View Drop Down
Senior Member
Senior Member
Avatar

Joined: 22 November 2004
Location: Switzerland
Status: Offline
Points: 336
Post Options Post Options   Thanks (0) Thanks(0)   Quote Fabian Quote  Post ReplyReply Direct Link To This Post Posted: 09 September 2010 at 11:05am
Thanks, Jason
 
it would be handy if the control itself has a global controls collection.
 
 
Product: Xtreme SuitePro (ActiveX) version 16.2.3

Platform: Windows 7 (32bit)

Language: Visual Basic 6.0 / SP6
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.189 seconds.