How to call a menuitem dynamically from code ?

Is it possible ? How ?
tks

Horacio,

Here is a quick example I use when I am offering some options to a user when they click on a + button.

   // Example Dynamic MENU
    Dim AddDeviceMenuChoice as new MenuItem
    AddDeviceMenuChoice.Append new MenuItem("Connect using device profile",0) // OPTION 0
    AddDeviceMenuChoice.Append new MenuItem("Quick connection",1) // OPTION 1
    
    dim AddDevicePopUpMenu As MenuItem=AddDeviceMenuChoice.PopUp
    if AddDevicePopUpMenu<>nil then
      if AddDevicePopUpMenu.Tag = 0 Then // OPTION 0
       // DO SOMETHING
        
      Elseif AddDevicePopUpMenu.Tag = 1 Then // OPTION 1
      // DO SOMETHING ELSE
      end if
      
    end if

thanks Mike but I need, if it’s possible, to know how to call an event by code.

[quote=147100:@Horacio Vilches]thanks Mike but I need, if it’s possible, something like " Call Menuitem_click" wich emulates the event of the user clicking the menuitem
in general, I also need to know how to call events by code.[/quote]
Generally a bad idea. It is better to put code that would run under an event into a shared method then in the event call the method. Then where ever else you need to call that code you just call the method.

so calling events is considered bad coding practice ?

move the code to be run into a method
then have the menu item call that method when you need it
and anything else that needs to do similarly can also call it

I think so. It is much easier to reuse code when the code is not embedded in UI controls.

It is a source of much grief when you decide to redesign a form and remove controls. Shared methods are unaffected by this and you just re-hook them up to the new controls as you need them.

I used to do this many years ago in VB5 and it caused me much pain. These days I do not fear tearing apart a form and starting over because the code is all safe to be reused as needed.

Not only is it bad practice, it is sometimes not even possible (such as in this case).

i only have code in the event if I only use the code once. if it is use in multiple place, i turn them into method.