How to change title of SubMenu item ???

Just starting out? Need help? Post your questions and find answers here.
agb2008
User
User
Posts: 60
Joined: Fri Jan 15, 2016 2:22 pm

How to change title of SubMenu item ???

Post by agb2008 »

It suppose to be an easy question but... How it is possible to change title of SubMenu (!) item created with OpenSubMenu ?
I could change main menu items text with SetMenuTitleText, I could change text for items in that menu with SetMenuItemText...
But if I've got a case when I created another SubMenu then I am getting strange situation - I could change text of items in that submenu,
but I can't change text for submenu setting itself.... Please advice...
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: How to change title of SubMenu item ???

Post by RSBasic »

Code: Select all

EnableExplicit

Define SubMenu

If OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If CreateMenu(1, WindowID(0))
    MenuTitle("Menu")
    SubMenu = OpenSubMenu("New")
    MenuItem(1, "File")
    MenuItem(2, "Folder")
    CloseSubMenu()
    SetMenuItemText(1, SubMenu, "Open")
  EndIf
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
:?:

\\Edit:
Only for Windows.
Image
Image
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to change title of SubMenu item ???

Post by mk-soft »

Only Windows :?:

Don´t work on MacOS :!:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: How to change title of SubMenu item ???

Post by Oma »

:?:
From PB-Help:
OpenSubMenu(Text$ [, ImageID])
...
Return value

None.
:?:
Regards, Charly
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
User avatar
mhs
Enthusiast
Enthusiast
Posts: 101
Joined: Thu Jul 02, 2015 4:53 pm
Location: Germany
Contact:

Re: How to change title of SubMenu item ???

Post by mhs »

RSBasic wrote:

Code: Select all

SubMenu = OpenSubMenu("New")
That seems to be a undocumented feature. The only way I know, is to close and re-create the whole menu with the other title...
agb2008
User
User
Posts: 60
Joined: Fri Jan 15, 2016 2:22 pm

Re: How to change title of SubMenu item ???

Post by agb2008 »

Look like for now I would just have to avoid using SubMenu option at all
until solution would be implemented in future versions of PB. :?
User avatar
mhs
Enthusiast
Enthusiast
Posts: 101
Joined: Thu Jul 02, 2015 4:53 pm
Location: Germany
Contact:

Re: How to change title of SubMenu item ???

Post by mhs »

mhs wrote:... is to close and re-create the whole menu with the other title...

Code: Select all

EnableExplicit

Procedure m_Menu(Title.s)

  If IsMenu(1) : FreeMenu(1) : EndIf
  
  If CreateMenu(1, WindowID(0))
    MenuTitle("Menu")
    OpenSubMenu(Title)
    MenuItem(1, "File")
    MenuItem(2, "Folder")
    CloseSubMenu()
  EndIf
  
  Debug "Menu created"

EndProcedure


If OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  m_Menu("New")

  AddWindowTimer(0, 1, 5000)

  Repeat
  
    Select WaitWindowEvent()
      Case #PB_Event_Timer
        m_Menu("Open")
      Case #PB_Event_CloseWindow
        End
    EndSelect
    
  ForEver
  
EndIf
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How to change title of SubMenu item ???

Post by Dude »

mhs wrote:The only way I know, is to close and re-create the whole menu with the other title...
Which is a totally ridiculous and impractical way to do it (not blaming you, mhs). An official command to change it would be best.
User avatar
mhs
Enthusiast
Enthusiast
Posts: 101
Joined: Thu Jul 02, 2015 4:53 pm
Location: Germany
Contact:

Re: How to change title of SubMenu item ???

Post by mhs »

I know that it's ridiculous :wink:

I would also prefer an official command, but if it's the only way at the moment... better than nothing. And my example shows that it's not impractical, depending on the requirements...

Is there a feature request?
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How to change title of SubMenu item ???

Post by Dude »

Also, as I mentioned in another thread, recreating a menu can fail if there's not enough memory at the time. Granted, this is a low possibility, but still there -- it happens to me with my laptop with 4 GB of RAM at times, because I have a bucketload of apps and things happening most of the time.
User avatar
charvista
Addict
Addict
Posts: 943
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: How to change title of SubMenu item ???

Post by charvista »

I played around just for fun by changing the menu completely with fictive words when clicking on the Refresh button, to see if there is a performance slow down. On my computer it is immediate, so it looks good. On Windows, it is necessary to use the API LockWindowUpdate_() to avoid flashing.

Code: Select all

Procedure.s CW(); Create Word
    C.s="bcdfghjklmnprstvwxz"
    V.s="aeiou"
    Syl=Random(5,2)
    For i=1 To Syl
        Wd.s+Mid(C,Random(Len(C),1),1)+Mid(V,Random(Len(V),1),1)
    Next i
    Wd.s=UCase(Left(Wd,1))+Right(Wd,Len(Wd)-1)
    ProcedureReturn Wd
EndProcedure

Procedure MenuA(Win,M)
    
    If IsMenu(M) : FreeMenu(M) : EndIf

    If CreateMenu(M,WindowID(Win))
        MenuTitle(CW()); first item on the menu bar
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        MenuTitle(CW()); second item on the menu bar
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        MenuTitle(CW()); third item on the menu bar
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        MenuTitle(CW()); fourth item on the menu bar
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
        OpenSubMenu(CW())
            For i=1 To 20
                MenuItem(i,CW())
            Next i
        CloseSubMenu()
    EndIf
EndProcedure

If OpenWindow(3, 0, 0, 800, 600, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
        ButtonGadget(2,650,500,80,30,"Refresh")
        MenuA(3,1)
        Repeat
            Event=WaitWindowEvent()
            Select Event
                Case #PB_Event_Gadget
                    Select EventGadget()
                        Case 2
                            LockWindowUpdate_(WindowID(3))
                            MenuA(3,1)
                            LockWindowUpdate_(0)
                    EndSelect
                Case #PB_Event_CloseWindow
                    End
            EndSelect
        ForEver
    EndIf
Dude said he had memory problems when modifying a menu... Does a menu use so much memory? I hope that you did remove the menu before re-creating a new one?
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: How to change title of SubMenu item ???

Post by RASHAD »

You can create 2 Menus with different text and switch between when you need
No free and recreate ,No lag time,No flicker
It is always there
I do not know if this trick will work with Mac or not
Oh I like Windows for such things (I can do what I want at any time)
Unless Danilo or Shardik can come with a solution for Linux & Mac

Code: Select all

If OpenWindow(0, 0, 0, 300, 300, "PureBasic - Menu",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)
   ButtonGadget(0,10,240,60,25,"Switch")

  If CreateMenu(0, WindowID(0))
    MenuTitle("File")
      MenuItem( 1, "&Load...")
      MenuItem( 2, "Save")
      MenuItem( 3, "Save As...")
      MenuBar()
      OpenSubMenu("Recents")
        MenuItem( 5, "Pure.png")
        MenuItem( 6, "Basic.jpg")
        OpenSubMenu("Even more !")
          MenuItem( 12, "Yeah")
        CloseSubMenu()
        MenuItem( 13, "Rocks.tga")
      CloseSubMenu()
      MenuBar()
      MenuItem( 7, "&Quit")

    MenuTitle("Edition")
      MenuItem( 8, "Cut")
      MenuItem( 9, "Copy")
      MenuItem(10, "Paste")
      
    MenuTitle("?")
      MenuItem(11, "About")
  EndIf   
  
  If CreateMenu(1, WindowID(0))
    MenuTitle("File")
      MenuItem( 1, "&Load...")
      MenuItem( 2, "Save")
      MenuItem( 3, "Save As...")
      MenuBar()
      OpenSubMenu("Recents #2")
        MenuItem( 5, "Pure.png")
        MenuItem( 6, "Basic.jpg")
        OpenSubMenu("Even more ! #2")
          MenuItem( 12, "Yeah")
        CloseSubMenu()
        MenuItem( 13, "Rocks.tga")
      CloseSubMenu()
      MenuBar()
      MenuItem( 7, "&Quit")

    MenuTitle("Edition")
      MenuItem( 8, "Cut")
      MenuItem( 9, "Copy")
      MenuItem(10, "Paste")
      
    MenuTitle("?")
      MenuItem(11, "About")
  EndIf
  HideMenu(1,1)
  HideMenu(0,0)
  
  Repeat
    Select WaitWindowEvent()
    
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            Run ! 1
            If Run = 1
                HideMenu(0,1)
                HideMenu(1,0)
            Else
                HideMenu(1,1)
                HideMenu(0,0)
            EndIf
        EndSelect

      Case #PB_Event_Menu

        Select EventMenu()  ; To see which menu has been selected
          Case 1
            Debug "Item 1"

          Case 11 ; About
            MessageRequester("About", "Cool Menu example", 0)
            
          Default
            MessageRequester("Info", "MenuItem: "+Str(EventMenu()), 0)

        EndSelect

      Case #PB_Event_CloseWindow
        Quit = 1

    EndSelect
  Until Quit = 1
EndIf
End  
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: How to change title of SubMenu item ???

Post by Dude »

charvista wrote:Dude said he had memory problems when modifying a menu
No, I said low memory can be problematic but the actual possibility of that is low.
charvista wrote:Does a menu use so much memory?
It depends on how big it is, and how many icons are used for the items. Menus can be massive.
User avatar
charvista
Addict
Addict
Posts: 943
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: How to change title of SubMenu item ???

Post by charvista »

Very interesting! Thanks a lot for the information kenmo! :wink:
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Post Reply