List all menus

Post

Posted
Rating:
#1 (In Topic #1265)
Regular
bill-lancaster is in the usergroup ‘Regular’
 I've done this before but am struggling now!
What is the best way to list all the menu items on a form?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 At a guess, I'd say that you'd need to parse the .form file?

Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
Use the Form.Menus property

Code (gambas)

  1.  
  2.   For Each hTopMenu As Menu in Form1.Menus
  3.    Print hTopMenu.Name, "Children: " & hTopMenu.Children.Count
  4.  
  5.     For Each hMenu As Menu In hTopMenu.Children
  6.       Print hTopMenu.Name & "/" & hMenu.Name, hMenu.Text, "Children: " & hMenu.Children.Count
  7.     Next
  8.  
  9.   Next
  10.  
  11.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
bill-lancaster is in the usergroup ‘Regular’
Thanks Bruce, but that lists only top menus and their immediate child menus, some of these could themselves have child menus and so on
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’

bill-lancaster said

Thanks Bruce, but that lists only top menus and their immediate child menus, some of these could themselves have child menus and so on

Yes i know.
Did you want me to write the complete function for you?  :roll:
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
I'd probably use a recursive function

Code (gambas)

  1.  
  2. Public Sub PrintAllMenus()
  3.  
  4.   For Each hTopMenu As Menu in Form1.Menus
  5.     PrintMenu(hTopMenu)
  6.   Next
  7.  
  8.  
  9. Public Sub PrintMenu(m As Menu)
  10.  
  11.   Dim mc As Menu
  12.   Dim sKeyPath As String = m.Name
  13.  
  14.   mc = m
  15.   Do  ' get menu path
  16.     If Not mc.Parent Then Break
  17.     mc = mc.Parent
  18.     sKeyPath = mc.Name &/ sKeyPath
  19.   Loop
  20.  
  21.  ' Print the current menu info
  22.   Print sKeyPath, m.Text, m.Shortcut
  23.  
  24.   If m.Children.Count Then  ' if the current item has children then recursively run this function
  25.     For Each mc As Menu In m.Children
  26.       PrintMenu(mc)
  27.     Next
  28.  
  29.  
  30.  

But I have no idea what you want to do with the info.
That's where you do your own coding. ;)
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Guru
cogier is in the usergroup ‘Guru’
I'm with 'thatbruce'. All the details are in the .form file: -

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim sText As String[] = Split(File.Load(User.Home &/ "Dropbox/gambas/ScreenShot/.src/FMain.form"), gb.NewLine, "", True)
  4.   Dim iLoop As Integer
  5.   Dim sMenus As New String[]
  6.   Dim bMenu As Boolean
  7.  
  8.   For iLoop = 0 To sText.Max
  9.     If Trim(sText[iLoop]) = "}" Then bMenu = False
  10.     If Trim(sText[iLoop]) Begins "\{" And InStr(sText[iLoop], "Menu") > 0 Then bMenu = True
  11.     If bMenu = True Then sMenus.Add(sText[iLoop])
  12.   Next
  13.  
  14.   Print sMenus.Join(gb.NewLine)
  15.  

This is some of the output from my program 'Screenhot"

 { MenuMain Menu
    Text = Shortcut(("File"), "F")
    Checked = True
    { MenuPrint Menu AllToolButtons
      Name = "MenuPrint"
      Text = Shortcut(("Print"), "P") & "…"
      Picture = Picture["icon:/medium/print"]
      Tag = "Menu"
      Shortcut = "Ctrl+P"
    { MenuExit Menu AllToolButtons
      Name = "MenuExit"
      Text = Shortcut(("Exit"), "E")
      Picture = Picture["icon:/medium/close"]
      Tag = "Menu"
      Shortcut = "Alt+F4"
  { MenuShot Menu
    Text = Shortcut(("Screen shot"), "S")
    { MenuCapture Menu AllToolButtons
      Name = "MenuCapture"
      Text = Shortcut(("Capture"), "C")
      Picture = Picture["icon:/medium/camera"]
      Tag = "Menu"
      Shortcut = "Ctrl+Shift+C"
Online now: Yes Back to the top

Post

Posted
Rating:
#8
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
If you want to parse the menu items to toggle 'enabled/disabled, I wrote this subroutine a few years ago to manage popup menus (right click or context menus)
You can set the menu item by 'block' or by text name. You can set multiple menu items buy sending each name separated by a comma.

example: The menu MyMenu contains the elements Menu1, Menu2, Menu3, Menu4.
  

Code (gambas)

  1.  SetMenuOptions(MyMenu, "Menu2,Menu4", False)
 
Subroutine:

Code (gambas)

  1. Private Sub SetMenuOptions(InMenu As Menu, InName As String, InMode As Boolean)
  2.  
  3.   Dim TmpMnu As Menu
  4.   Dim TmpAry As String[]
  5.   Dim TmpInt As Integer
  6.  
  7.   If InName = "" Then 'Set ALL menus to the required mode.
  8.     For Each TmpMnu In InMenu.Children
  9.       TmpMnu.Enabled = InMode
  10.     Next
  11.   Else 'Set selected menus (by name) to the required mode.
  12.     TmpAry = Split(InName, ",")
  13.     For Each TmpMnu In InMenu.Children
  14.       For TmpInt = 0 To TmpAry.Max
  15.         If String.UCase(TmpMnu.Text) = String.UCase(TmpAry[TmpInt]) Then TmpMnu.Enabled = InMode
  16.       Next
  17.     Next
  18.  

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#9
Regular
bill-lancaster is in the usergroup ‘Regular’
 Thank you for the solutions, the solution from BruceS was the one I was looking for, but the thatbruce/cogier solution is very interesting.
Thank you once again.
Bill
Online now: No Back to the top

Post

Posted
Rating:
#10
Guru
BruceSteers is in the usergroup ‘Guru’
Be aware if you use the .form file reading option then the source directory path must always be available.
If you use an absolute path to the .form file then the folder can not be moved unless you change the path in the code too.

Or If you use relative ./paths and then make a standalone .gambas executable archive file (not in the project folder) then the .form files are not included in the .gambas archive so that will also fail.

My method will be without any of those problems :)

Also my method will be "live/current"
Ie. Toggle/Radio menu Values will show as they are currently set not as they are initially configured.
and additional menus added by code will also be detected.
Online now: No Back to the top

Post

Posted
Rating:
#11
Regular
bill-lancaster is in the usergroup ‘Regular’
Thanks Bruces
Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
Yes, your points are taken, moot.
Howsomeever, I have a little mind experiment that may destroy both our theories. (Sorry, I've just been viewing a Brian Cox video and if you think you understand quantum physics then both you and I are mad.)

Suppose you have a custom component that is a menu… it isnt in the form nor is it in the menus…

Just to stick my tongue out, we have used such "virtual menu" components for years now. :-)
The are really cool for things like AboutMe's, SystemInfo's, and other things that we can just drag onto a form as a control with no code needed and yet when the proggie runs, woof! there is a menu item.

sleep now
b

Online now: No Back to the top
1 guest and 0 members have just viewed this.