B4J Question B4J MENUBAR How to simulate item selection from code ?

focus330

Member
Licensed User
Longtime User
Hi

As i wrote in the question how to simulate selection of an item from code ?
All the items are cretaed from code and I have a sub like this:
MenuBar:
Public Sub Menu_Action(MI As MenuItemTextClass)
    
[CODE=b4x]Public Sub MenuAction(MI As MenuItemTextClass)
    Select Case MI.Gettag
        Case "Param01"
MI.SetEnabled(False)
Param01.Show(MainForm)[/CODE]

For example I want to launch "Param01" from code

Thank you
 

stevel05

Expert
Licensed User
Longtime User
You could delegate the select to a separate sub that accepts a string parameter, then call that from code.

B4X:
Public Sub Menu_Action(MI As MenuItemTextClass)
    MenuActionDo(MI.Tag)
End Sub

Private Sub MenuActionDo(Option As String)
    Select Case Option
        Case "Param01"
           'Whatever
    End Select
End Sub

Or move whatever is in the select block for the option into a separate sub and call it directly.

B4X:
Public Sub Menu_Action(MI As MenuItemTextClass)
    Select Case MI.Tag
        Case "Param01"
          Param01Do
    End Select
End Sub

Private Sub Param01Do
    'Existing code from select block
End Sub
 
Last edited:
Upvote 1

focus330

Member
Licensed User
Longtime User
You could delegate the select to a separate sub that accepts a string parameter, then call that from code.

B4X:
Public Sub Menu_Action(MI As MenuItemTextClass)
    MenuActionDo(MI.Tag)
End Sub

Private Sub MenuActionDo(Option As String)
    Select Case Option
        Case "Param01"
           'Whatever
    End Select
End Sub

Or move whatever is in the select block for the option into a separate sub and call it directly.

B4X:
Public Sub Menu_Action(MI As MenuItemTextClass)
    Select Case Option
        Case "Param01"
          Param01Do
[CODE=b4x]    If A = B then

            Dim MI1 As MenuItemTextClass

            MI1.inizialize

            MI1.SetTag(""Param01")

            Main.Menu_Action(MI1) ........

[/CODE][/CODE]
End Select
End Sub

Private Sub Param01Do
'Existing code from select block
End Sub
Propose code:
Private MyCode as sub   

...

[/QUOTE]
Yes. Thanks. But I want ro simultate a user selection.
 Perhaps sometink like this:

[CODE lang="b4x" title="Add code"]    If A = B then

            Dim MI1 As MenuItemTextClass

            MI1.inizialize

            MI1.SetTag(""Param01")

            Main.Menu_Action(MI1) ........

[/CODE][/CODE]

Is it correct ?
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
As i wrote in the question how to simulate selection of an item from code ?
The Sub Menu_Action routine uses the variable MI to indicate which menu option the user has clicked. You can put in a separate sub routine what should be performed as a function when a menu option is clicked. This separates the program flow and the function to be performed. Your programming code will become more compact and now and later easier to maintain and better to understand, even with many menu options.

Back to your question "how to simulate selection", that's just calling the subroutine function without needing to use the Sub Menu_Action routine at all.
 
Upvote 0
Top