Print Page | Close Window

Iterating all controls

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Command Bars
Forum Description: Topics Related to Codejock Command Bars
URL: http://forum.codejock.com/forum_posts.asp?TID=17221
Printed Date: 23 November 2024 at 5:09pm
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: Iterating all controls
Posted By: Fabian
Subject: Iterating all controls
Date 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



Replies:
Posted By: jpbro
Date 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



Posted By: Fabian
Date 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


Posted By: jpbro
Date 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



Posted By: Fabian
Date 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



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net