[Gambas-user] Problem with code made menu event handling

Tobias Boege taboege at ...626...
Sun Oct 7 09:51:04 CEST 2012


> > With 1 AddOn in the file (lets say AddOnOne) the menu shows AddOnOne and
> > when clicking runs the application AddOnOne
> > 
> > With 2 AddOns in the file (lets say AddOnOne and AddOnTwo) the menu
> > shows AddOnOne and AddOnTwo and When clicking either one of them
> > AddOnTwo is ALWAYS run.
> > 
> > This somehow makes sense to me looking at my code.
> 
> See my notes in your code as to why.
> 
> > But how do I determine what menu item is clicked 
> 
> This is the real problem. Actually, I don't know if there is a way. This
> is also the reason that I used that addinmeta class. It essentially acts
> as a wrapper for the native Menu class and handles its' own events (via
> that SELF observer.  That way each addinmeta instance handles itself and
> "knows" what has been clicked.
> 
> Doing this in a module as you have done maybe feasible but I found it
> easier to take the above approach.
> 
> 
> >     For iNdex = 1 To AddOnNumber
> >       hMenuItem = New Menu(hMenu) As "_MenuEvent"
> >       With hMenuItem
> >         .Name = "mnu" & AddOnName[iNdex]
> >         .Enabled = True
> >         .Caption = AddOnName[iNdex]
> >         .Text = AddOnName[iNdex] & "..."
> >       End With
> >     Next
> >   Endif
> >   If hMenu.Children.Count = 0 Then
> >     hMenu.Visible = False
> >   Endif
> > 
> >   
> > End
> > 
> > Public Sub _MenuEvent_Click()
> >   
> >   Dim iX, iLength As Integer
> >   Dim AddOnToRun As String
> 
> When this is run, hMenuItem will be the last item you added, therefore
> no matter how many Add-Ins you load only the last one will be run.
> So you would need to find the menu item that was clicked and set
> hMenuItem to that.
> 
> >   iLength = Len(hMenuItem.Caption)
> >   AddOnToRun = Left$(hMenuItem.Caption, (iLength - 3))
> >   AddOnToRun = AddOnToRun & ".gambas"
> >   Message(AddOnToRun)
> >   AddOns.Run(AddOnToRun)
> >   
> > End

Couldn't you use 'Last' instead of hMenuItem in _MenuEvent_Click()?

Regards,
Tobi




More information about the User mailing list