B4J Question Menu not actioning action.

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

This is my first attempt at a menu, starting with the Tutorial and the default text when a menu is added I have produced the code below. I thought that this is so simple it could not fail, I was wrong.

Menu Items from the Visual Designer
B4X:
[
    {Text: "_Information", Children:
        [
            "_Help", "_About"
        ]
    }
]

The "About" Sub that should be actioned.
B4X:
Sub About_action
    Log("About_action")
End Sub

When I click on the "About" item in the menu nothing happens.
Obviously I have done something wrong, any ideas?

Regards Roger
 

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks Erel,

I sorted it out with the code below. I will have another look at setting the EventName property next year.
There must be a better way of designing menus. Arcane JSON strings or convoluted subs calling subs? [WishList item]

B4X:
Sub MenuBar1_Action
   Dim Item As MenuItem = Sender
   If Item.Text.Contains("About") Then About_action
   If Item.Text.Contains("Help") Then Help_action
End Sub

Regards Roger
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
Roger,
you can use the eventtag in json (like Erel pointed out before):
B4X:
[
  {Text: "File", Children:
   [{Text: "New", EventName: "mnuNew", Tag: "mnuNew"},
   "-",
   {Text: "Open", EventName: "mnuOpen", Tag: "mnuOpen"},
   "-",
   {Text: "Save", EventName: "mnuSave", Tag: "mnuSave"},
   "-",
   {Text: "Close", EventName: "mnuClose", Tag: "mnuClose"}]
  },
  {Text: "Information", Children:
  [
  {Text: "Help", EventName: "mnuHelp", Tag: "mnuHelp"},
   "-",
   {Text: "About", EventName: "mnuAbout", Tag: "mnuAbout"}
  ]
  }
]
and then later in your code module something like this:
B4X:
Sub  mnuAbout_Action
   Dim s As String
'    Log("About")
   Dim mf As ModalForm
   mf.Initialize( MainForm, "frmAbout")
  s =  mf.Show
End Sub
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Nope, new year and it still doesn't come together.

I use the following in "Menu Items" in the Visual designer and when I try to compile the Logs tell me I have an "Unterminated array at charactre 53 ...".

[
{Text: "_Information", Children:
[
(Text: "Help",Enabled: True),
{Text:"About", EventName: "About"}
]
}
]

Any suggestions?

Regards Roger
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

does this example help?
Simple Example of a MenuBar
B4X:
MenuItems
File
  New
  Save
  Exit
Edit
  Copy
  Paste
  Cut
Help
  About

Visual Designer MenuBar Properties > Menu Items JSON String
B4X:
[
{Text: "_File",
  Children:
  [
  {Text:"_New", Tag:"FileNew", EventName: "FileNew"},
  {Text: "_Save", Tag:"FileSave", EventName: "FileSave"},
  {Text: "_Exit", Tag:"FileExit", EventName: "FileExit"}
  ]
},
{Text: "_Edit",
  Children:
  [
  {Text:"_Copy", Tag:"EditCopy", EventName: "EditCopy"},
  {Text:"_Paste", Tag:"EditPaste", EventName: "EditPaste"},
  {Text:"_Cut", Tag:"EditCut", EventName: "EditCut"}
  ]
},
{Text: "_Help",
  Children:
  [
  {Text: "_About", Tag: "HelpAbout", EventName: "HelpAbout"}
  ]
}
]

B4J Events
Note: Not all defined.

B4X:
Sub FileNew_Action
  Log("FileNew clicked.")
End Sub

Sub FileSave_Action
  Log("FileSave clicked.")
End Sub

Sub FileExit_Action
  Log("FileExit clicked.")
  ExitApplication
End Sub

Sub HelpAbout_Action
  Log("HelpAbout clicked.")
End Sub

Output Examples

FileNew clicked.
FileSave clicked.
FileExit clicked.
...
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi Rob,

Thanks for the reply. Your example is pretty much the same as I started with originally, however I re-did the exercise with your example and ended with this:
[
{Text: "_Information",
Children:
[
{Text:"_Help", Tag:"FileNew", EventName: "Help"},
{Text: "_About", Tag:"FileSave", EventName: "About"}
]
}
[

It works, I have compared the two and finally spotted my error in the original.:oops:

Thank heaps.
Regard Roger
 
Upvote 0
Top