Example Popup Menu help please

Post

Posted
Rating:
#1 (In Topic #1267)
Trainee
 Hi, first time poster, I am currently working on a launcher for Linux games, but I need to add a context menu (popup menu) to the ListView control (well any controls in general but that'll get me started).

I have looked and not been able to find any examples that show me how to use the menu's, the help file is good, but I do need code to see it all in action.

Would somebody have time to write up an example of making a dynamic created popup menu that activated at the mouse position (I can limit it's off screen stuff etc after), but I just want to be able to right click the form (for the example) and have it show a menu containing "Item 1", "Item 2", "Separator", "Item 3" and how I capture the event when I pick them.

I did figure out how to do this in vb6 and Xojo, but gambas doesn't have much example code to learn from, that I've found and the help doesn't seem to work on my Linux Mint either, so maybe that's where all the info is I need.

Thanks for any help, this is the last thing I need to add to my launcher, but I've not even got my code to run whenever I try to create a dynamic menu (or even a fixed one), I just don't understand how to do it.

Happy to share my app/code once I am happy with it, it does have some cool ideas I figured out over the years to handle data and dynamic columns.
Online now: No Back to the top

Post

Posted
Rating:
#2
Trainee
Never mind, figured it all out :)

I just had to set the Text of the menu items and up it popped.

I'll see how I go detecting the actions, but I don't see it being a problem now.
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee

Code

  Dim ContMenu As Menu
  Dim MenuItem1 As Menu
  Dim MenuItem2 As Menu
  Dim MenuItem3 As Menu
  
  Dim Result As - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  Dim HitItem As String
  
  ContMenu = New Menu(LastOSLinuxStoreMain, False)
  
  MenuItem1 = New Menu(ContMenu, False)
  MenuItem1.Text = "Test 1 Menu"
  
  MenuItem2 = New Menu(ContMenu, False)
  MenuItem2.Text = "Test 2 Menu"
  
  MenuItem3 = New Menu(ContMenu, False)
  MenuItem3.Text = "Test 3 Menu"
  
  Result = ContMenu.Popup(Mouse.ScreenX, Mouse.ScreenY)
  HitItem = ContMenu. - - - - - - - - - - - - - - - - - - - - - - - - - -
  Message(HitItem)

My bad, I still can't figure out how to catch the clicked items, anyone? I assume I either have to capture it as a type set at the top in the - - - - - - - line or check a result at the bottom with - - - - - changed to something, or maybe I am way off?
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
Something like this maybe

Code (gambas)

  1.  
  2. Private $hPopup As Menu  ' this is the popup menu
  3.  
  4. Public Sub Form_Open()
  5.  
  6.   $hPopup=New Menu(Me, True)  ' create the menu as a hidden one.
  7.  
  8.  
  9. Public Sub Form_MouseUp()
  10.  
  11.   Dim hMenu As Menu
  12.   If Not Mouse.Right Then Return  ' we only want right clicks
  13.  
  14.   $hPopup.Children.Clear  ' clear main popup menu parent items
  15.  
  16. ' i just make some menus here. i guess for your needs the creation would be more dynamic/varied.
  17.  
  18.   hMenu=New Menu($hPopup) As "POP"  ' make a menu item in $hPopup and use event name POP
  19.   hMenu.Name="M1"
  20.   hMenu.Text = "Item 1"  ' make text
  21.  
  22.   hMenu=New Menu($hPopup) As "POP"  ' make another menu item in $hPopup and again use event name POP
  23.   hMenu.Name="M2"
  24.   hMenu.Text = "Item 2"  ' make text
  25.  
  26.   hMenu=New Menu($hPopup)  ' make a menu item in $hPopup with no event as it's a spacer
  27.   ' do not enter any text so it becomes a spacer
  28.  
  29.   hMenu=New Menu($hPopup) As "POP"  ' make another menu item in $hPopup and again use event name POP
  30.   hMenu.Name="M3"
  31.   hMenu.Text = "Item 3"  ' make text
  32.  
  33.   $hPopup.Popup  ' show the menu at mouse position
  34.  
  35.  
  36. Public Sub POP_Click()  ' all menus share event POP so in the event we detect the menu name to distinguish between them
  37.  
  38.  Select Last.Name
  39.   Case "M1"
  40.   Print "Menu 1 was selected"
  41.   Case "M2"
  42.   Print "Menu 2 was selected"
  43.   Case "M3"
  44.   Print "Menu 3 was selected"
  45.  
  46.  
  47.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee
Thank you, I'll give it a go and see if it works like I intended. I was getting close but was stumped.

-EDIT-

Yes, works perfect, thanks again for your time :)
Online now: No Back to the top

Post

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

LiveFreeDead said

Thank you, I'll give it a go and see if it works like I intended. I was getting close but was stumped.

You're welcome.

PS. if you want example code try the software farm (a link is on the gambas IDE welcome screen) there's tons of example software there from pretty simple to pretty advanced :)
plus there's also programs on the wiki /app - Gambas Documentation
Online now: No Back to the top

Post

Posted
Rating:
#7
Trainee
 Software Farm will give me what I need to find in the future, I hope to not have to bother you guys too much.

I have a few bugs I can report, is there somewhere I should do that? BTW I found work arounds, but I am always for making tools as bug free as we can.
Online now: No Back to the top

Post

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

LiveFreeDead said

Software Farm will give me what I need to find in the future, I hope to not have to bother you guys too much.

I have a few bugs I can report, is there somewhere I should do that? BTW I found work arounds, but I am always for making tools as bug free as we can.

The bugtracker is at Connexion  you register then can report bugs there.
also see this page…
/doc/report - Gambas Documentation

You should peruse the wiki some more though. lots of info there :)  / - Gambas Documentation
(the link i posted above for reporting bugs is on the main page)

But don't worry about asking questions , that's what we are here for :)
You just said you wanted examples , so there's examples :)
Online now: No Back to the top

Post

Posted
Rating:
#9
Trainee
Would it be hard to add a sub menu at the bottom with 2 more items in it?

The plan is to Have

Select -
-    All
-    None
-    Invert
Hide
-    Portable
-    Integrated
Load Preset
Save Preset
.
.
.

So a Sub menu for each action and a few root menu choices, like loading from a preset etc. so Sub menu's I assume would require a 2nd main menu with it parented to the 1st main menu?, I could still use the same POP action with a different name applied? :) - but I am just guessing
Online now: No Back to the top

Post

Posted
Rating:
#10
Trainee
Yep I figured it out, was fairly easy :)

The trick was giving the root menu it's Text and making it not hidden and populate it with items, then it passes to POP_Click and did it's thing
Online now: No Back to the top

Post

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

LiveFreeDead said

Yep I figured it out, was fairly easy :)

The trick was giving the root menu it's Text and making it not hidden and populate it with items, then it passes to POP_Click and did it's thing

but if it's not hidden then it always shows in the applications menubar so I don't think that was the trick, just a workaround.
(it's fine if you want it on the applications menubar)

Sub menus are done just by changing the parent when creating them.

Eg for your menus.
Select -
- All
- None
- Invert
Hide
- Portable
- Integrated
Load Preset
Save Preset

Code (gambas)

  1.  
  2. Dim hSubParent As Menu = New Menu($hPopup)  ' the Select menu parent
  3. hSubParent.Text = "Select"
  4.  
  5. ' add the next ones to hSubParent not $hPopup
  6. Dim hSub As Menu = New Menu(hSubParent) As "POP"
  7. hSub.Text = "All"
  8. hSub.Name = "mnuSelectAll"
  9. hSub.Shortcut = "Ctrl+A"
  10.  
  11. hSub = New Menu(hSubParent) As "POP"
  12. hSub.Name = "mnuSelectNone"
  13. hSub.Text = "None"
  14.  
  15. hSub = New Menu(hSubParent) As "POP"
  16. hSub.Name = "mnuSelectToggle"
  17. hSub.Text = "Invert"
  18.  
  19. hSubParent = New Menu($hPopup)  ' the Hide menu parent
  20. hSubParent.Text = "Hide"
  21.  
  22. hSub = New Menu(hSubParent) As "POP"
  23. hSub.Name = "mnuHideP"
  24. hSub.Text = "Portable"
  25.  
  26. hSub = New Menu(hSubParent) As "POP"
  27. hSub.Name = "mnuHideI"
  28. hSub.Text = "Intergrated"
  29.  
  30. ' the last 2 are children of the main $hPopup
  31. hSub = New Menu($hPopup) As "POP"
  32. hSub.Name = "mnuLoadP"
  33. hSub.Text = "Load Preset"
  34.  
  35. hSub = New Menu($hPopup) As "POP"
  36. hSub.Name = "mnuSaveP"
  37. hSub.Text = "Save Preset"
  38.  
  39.  
Online now: No Back to the top

Post

Posted
Rating:
#12
Trainee
I've tried to sign up to the bug tracker but my first attempt I never received the confirmation email, so this morning I tried again with my outlook address, I got the confirmation email, but when I click the link I got "Unable to activate account." I tried clicking it again with same results.

Please if you have contact with the site admin, could they enable my account manually, either LiveFreeDead or GlennChugg. I can use either as they both have correct credentials.

Thanks again for clarification on the menu systems of Gambas, I will finish off my project and see if I can help out around here in the forums, my methods are never as clean as yours, but if I can help when I do know something, I will :)
Online now: No Back to the top

Post

Posted
Rating:
#13
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’

LiveFreeDead said

I've tried to sign up to the bug tracker but my first attempt I never received the confirmation email, so this morning I tried again with my outlook address, I got the confirmation email, but when I click the link I got "Unable to activate account." I tried clicking it again with same results.

Please if you have contact with the site admin, could they enable my account manually, either LiveFreeDead or GlennChugg. I can use either as they both have correct credentials.

Have you tried logging in with your account at bugtracker?
If that doesn' t work subscibe to the user mailing list an report your problem there.
Then Benoit can take care of it

gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
#14
Trainee
 Yeah the 2nd attempt did log in fine today, the activation link should log you in or at least not show that it's failed though. It takes a lot of time to gather all the information for a clear bug report, so I really didn't want to attempt to do it when I wasn't confident it would even work.
Online now: No Back to the top
1 guest and 0 members have just viewed this.