B4J Question Using MenuItem.Tag to Dynamically Call Modules in MenuBar1_Action

jroriz

Active Member
Licensed User
Longtime User
I'm building a long menu using MenuBar in B4J, and in the MenuBar1_Action event, I want to avoid writing a long If...Then or Select Case block for each menu item.

All my menu items will eventually call the same method like this:
B4X:
x.Show("Module", m.Text)

Each MenuItem has a Tag, for example:
JSON:
{
    Text: "Consulta com IA",
    Tag: "consultaIA, Consulta com IA"
}

In the event:
B4X:
Private Sub MenuBar1_Action
    Dim m As MenuItem = Sender
    If m.Text = "Consulta com IA" Then ConsultaIA.Show("ConsultaIA", m.Text)
End Sub

Is it possible (and safe) to use the Tag property to automate these calls, maybe splitting the tag and calling the corresponding module dynamically?

Any suggestions or best practices?
 

Cableguy

Expert
Licensed User
Longtime User
Private Sub MenuBar1_Action Dim m As MenuItem = Sender If m.Text = "Consulta com IA" Then ConsultaIA.Show("ConsultaIA", m.Text) End Sub
You can change that into:


B4X:
Private Sub MenuBar1_Action
    Dim m As MenuItem = Sender
    try
    CallSub (ME, sender.tag) 'Me is the module the sub you want to call exist'
    catch
    end try
End Sub
edit: this is pseudo code, you need to adjust to your needs
 
Upvote 0
Top