Page 1 of 1

Add items in popup menu with no doubloon [Resolved]

Posted: Wed Jan 20, 2010 12:42 pm
by Kwai chang caine
Hello at all

I try to add items in popup menu, so i don't want two times the same item :roll:
So i have create a procedure for testing if the new items is already in the menu.

Obviously if i write this post, it's that don't works like usually :?

I have see that the API "GetMenuItemCount_" not refresh after the adding :shock: beccause the result is always at 1 then the news items is good added :shock:

Code: Select all

Enumeration
 #Form
 #MenuContextuel
 #MenuCtx1
EndEnumeration

Dim NewContext.s(7)
Define Menu.MENUITEMINFO
Global HwndMenu

Procedure EstGroupeContextuel(Groupe.s)

 MaxGroupe = 0
 NbreItem = GetMenuItemCount_(MenuID(#MenuContextuel))
 Debug NbreItem

 For i = 0 To NbreItem + 1
   
  a = GetMenuItemID_(MenuID(#MenuContextuel), i)

  If UCase(GetMenuItemText(#MenuContextuel, a)) = UCase(Groupe)
   ProcedureReturn #True
  EndIf
  
 Next 
  
 ProcedureReturn #False
 
EndProcedure

NewContext(1) = "Hello"
NewContext(2) = "It's"
NewContext(3) = "always"
NewContext(4) = "always"
NewContext(5) = "Kcc"
NewContext(6) = ":-)"
NewContext(7) = ":-)"

OpenWindow(#Form, 100, 150, 250, 260, "PureBasic - Menu")
CreatePopupMenu(#MenuContextuel)
MenuTitle("Menu context one")
MenuItem(#MenuCtx1, "Item one of menu context one")
HwndMenu = GetSubMenu_(MenuID(#MenuContextuel), 0)

For i = 1 To 7

 If Not EstGroupeContextuel(NewContext(i))
  
  With Menu
   \cbSize = SizeOf(MENUITEMINFO)
   \fMask = #MIIM_TYPE
   \fType = #MFT_STRING
   \dwTypeData = @NewContext(i)
  EndWith
    
  InsertMenuItem_(HwndMenu, i, #MF_BYPOSITION, Menu) ; Remplace i par -1 pour mettre l'item en dernier
  
 EndIf

Next

Repeat
 
 Select WaitWindowEvent()
    
  Case #WM_RBUTTONDOWN
  
   DisplayPopupMenu(#MenuContextuel, WindowID(#Form))
 
  Case #PB_Event_CloseWindow
  
   Quit = 1   

 EndSelect
 
Until Quit = 1
Thanks for your help and have a good day

Re: Add items in menu, GetMenuItemCount_ not refresh :-(

Posted: Wed Jan 20, 2010 3:03 pm
by IdeasVacuum
Hello KCC

As you noted, in the Procedure EstGroupeContextuel(Groupe.s):

Code: Select all

a = GetMenuItemID_(MenuID(#MenuContextuel), i)
a always returns -1

Therefore the left side of the test always has an empty string and equality is never #TRUE:

Code: Select all

UCase(GetMenuItemText(#MenuContextuel, a))
There is API code rather than PB that you are using to get the sub-menu exactly as you require it. I do not fully understand the API code logic, but I can show how to make the test work via a dumbed-down version of your code and maybe that will help you get to where you want to be:

Code: Select all

#False = 0;
#True = 1;

Enumeration
#Form
#Menu
EndEnumeration

   iNewItemsCnt = 1
iMenuItemsTotal = 1
  iTotalNewItems = 7

Dim NewContext.s(iTotalNewItems)

Procedure EstGroupeContextuel(Groupe.s)

Shared iMenuItemsTotal

     For i = 0 To iMenuItemsTotal
   
            If UCase(GetMenuItemText(#Menu, i)) = UCase(Groupe)
  
                       ProcedureReturn #True
   
             EndIf
 
    Next
 
ProcedureReturn #False

EndProcedure


NewContext(1) = "Hello"
NewContext(2) = "It's"
NewContext(3) = "always"
NewContext(4) = "always"
NewContext(5) = "Kcc"
NewContext(6) = ":-)"
NewContext(7) = ":-)"


OpenWindow(#Form, 100, 150, 250, 260, "PureBasic - Menu")
CreatePopupMenu(#Menu)
MenuTitle("Menu Title")
OpenSubMenu("SubMenu")


For iNewItemsCnt = 1 To iTotalNewItems

        If Not EstGroupeContextuel(NewContext(iNewItemsCnt))
 
                iMenuItemsTotal = iMenuItemsTotal + 1
   
                MenuItem(iMenuItemsTotal, NewContext(iNewItemsCnt))
 
        EndIf

Next

Repeat

         Select WaitWindowEvent()
   
              Case #WM_RBUTTONDOWN
 
                    DisplayPopupMenu(#Menu, WindowID(#Form))

              Case #PB_Event_CloseWindow
 
                    Quit = 1   

         EndSelect

Until Quit = 1
.... or maybe not :?

Code: Select all

a = GetMenuItemID_(HwndMenu, i)
returns more index numbers

Re: Add items in menu, GetMenuItemCount_ not refresh :-(

Posted: Wed Jan 20, 2010 3:41 pm
by Kwai chang caine
Thanks IDEASVACUUM

Yes you have right, it's all in API because i want not use an array for the PB ID :roll:
With an array it's very more simple, it's sure :wink:

The problem why i have choose the API way, it's that all the time of program i add and remove items in the popup menu.
And i would be sure, not have error when i delete or add items.
Furthermore i'm forced to use sometime API, because the REMOVE function is not PB native :(

I nearly sure i'm not so far of the solution.... :roll:
It's a pity....if it's not possible or so hard to manage menu with API...i choose your solution :(

Thanks for your help 8)

Re: Add items in popup menu with no doubloon

Posted: Wed Jan 20, 2010 3:55 pm
by Kwai chang caine
I move a little bit :oops:

I have fixed the refresh problem
In fact i confused the MenuID() and the handle of menu :oops:

But now, i return always the first sentence "Hello", in my procedure "IsInPopUpMenu()" :shock:

Code: Select all

Enumeration
 #Form
 #MenuContextuel
 #MenuCtx1
 #MenuCtx2
EndEnumeration

Dim NewContext.s(7)
Global Menu.MENUITEMINFO
Global HwndMenu

Procedure IsInPopUpMenu(Groupe.s)

 NbreItem = GetMenuItemCount_(HwndMenu)
 Debug NbreItem
 
 For u = 0 To NbreItem + 1
   
  a = GetMenuItemID_(HwndMenu, u)
  Debug GetMenuItemText(#MenuContextuel, a)
  
  If Trim(UCase(GetMenuItemText(#MenuContextuel, a))) = Trim(UCase(Groupe))
   ProcedureReturn #True
  EndIf
 
 Next 

 ProcedureReturn #False
 
EndProcedure

NewContext(1) = "Hello"
NewContext(2) = "It's"
NewContext(3) = "always"
NewContext(4) = "always"
NewContext(5) = "Kcc"
NewContext(6) = ":-)"
NewContext(7) = ":-)"

OpenWindow(#Form, 100, 150, 250, 260, "PureBasic - Menu")
CreatePopupMenu(#MenuContextuel)
MenuTitle("Menu context one")
MenuItem(#MenuCtx1, "Item one of popup menu one")
MenuItem(#MenuCtx2, "Item two of popup menu one")
HwndMenu = GetSubMenu_(MenuID(#MenuContextuel), 0)

For i = 1 To 7

 If Not IsInPopUpMenu(NewContext(i))
  
  With Menu
   \cbSize = SizeOf(MENUITEMINFO)
   \fMask = #MIIM_TYPE
   \fType = #MFT_STRING
   \dwTypeData = @NewContext(i)
  EndWith
 
  InsertMenuItem_(HwndMenu, -1, #MF_BYPOSITION, Menu) 
 
 EndIf

Next

Repeat
 
 Select WaitWindowEvent()
    
  Case #WM_RBUTTONDOWN
  
   DisplayPopupMenu(#MenuContextuel, WindowID(#Form))
 
  Case #PB_Event_CloseWindow
  
   Quit = 1   

 EndSelect
 
Until Quit = 1

Re: Add items in popup menu with no doubloon

Posted: Wed Jan 20, 2010 4:37 pm
by Trond

Code: Select all

Enumeration
#Form
#MenuContextuel
#MenuCtx1
#MenuCtx2
EndEnumeration

Dim NewContext.s(7)
Global Menu.MENUITEMINFO
Global HwndMenu

Procedure IsInPopUpMenu(Groupe.s)
  NbreItem = GetMenuItemCount_(HwndMenu)
  For u = 0 To NbreItem-1
    
    text.s = Space(255)
    GetMenuString_(HwndMenu, u, @text, 255, #MF_BYPOSITION)
    
    If Trim(UCase(text)) = Trim(UCase(Groupe))
     ProcedureReturn 1
    EndIf
    
  Next
  ProcedureReturn 0
EndProcedure

NewContext(1) = "Hello"
NewContext(2) = "It's"
NewContext(3) = "always"
NewContext(4) = "always"
NewContext(5) = "Kcc"
NewContext(6) = ":-)"
NewContext(7) = ":-)"

OpenWindow(#Form, 100, 150, 250, 260, "PureBasic - Menu")
CreatePopupMenu(#MenuContextuel)
MenuTitle("Menu context one")
MenuItem(#MenuCtx1, "Item one of popup menu one")
MenuItem(#MenuCtx2, "Item two of popup menu one")
HwndMenu = GetSubMenu_(MenuID(#MenuContextuel), 0)

For i = 1 To 7
  If Not IsInPopUpMenu(NewContext(i))
    With Menu
     \cbSize = SizeOf(MENUITEMINFO)
     \fMask = #MIIM_TYPE
     \fType = #MFT_STRING
     \dwTypeData = @NewContext(i)
    EndWith
    InsertMenuItem_(HwndMenu, -1, #MF_BYPOSITION, Menu) 
  EndIf
Next


Repeat
  Select WaitWindowEvent()
    Case #WM_RBUTTONDOWN
      DisplayPopupMenu(#MenuContextuel, WindowID(#Form))
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver
Image

Re: Add items in popup menu with no doubloon

Posted: Wed Jan 20, 2010 4:59 pm
by IdeasVacuum
Ah, Trond has it. GetMenuItemText(#MenuContextuel, a) finds the items added with PB code but not those added with the API code.

Re: Add items in popup menu with no doubloon

Posted: Wed Jan 20, 2010 5:11 pm
by Kwai chang caine
Yaaoooouuu !!!
TROND you are my saver !!!

Since this morning i search on this code....this is the proof :oops:

Image

And like you see....not really find
I have always say that....you are really one of the BEST 8)
I have never say that to you...but you are also one of my heroes :oops:

I thanks you very very much
Image

I don't know you are a CAT :lol:
It's perhaps for that you always falling on your four footed in programming :lol:

Long life at TROND the magnificient 8)

I wish you, the most very good day of the world :D