B4J Question help with menubar

omaroski

Member
Licensed User
Longtime User
Hello,

i don't understand how to work with the menubar item. For example:

- how to read from code if a menu item is checked or not?
- how to enable/disable a menu item from code?

I understand how to write the JSON string

example:
{Text: "_Help", Children:
        [
            {Text: "Disabled Item", Enabled: False},
            {Text: "Item With Unique Event Name",Selected: False, EventName: "mnu1234"},
            {Text: "Item With Tag", Tag: "MyTag"}
        ]
    }

- how to read from code if "Item With Unique Event Name" is checked/unchecked?
I understand i can get the action with:
B4X:
Sub mnu1234_Action

End Sub

- how do i enable "Disabled Item" or read if enabled or not from code?

Thank you!
 

PaulMeuris

Active Member
Licensed User
Here's some test code with a default menu bar:
B4X:
'[
'    {Text: "_File", Children:
'        [
'            "_New", "_Save",
'            {Text: "_Export", Children: ["Zip File", "Tar File"]},
'            "-",
'            "_Close"
'        ]
'    },
'    {Text: "_Edit", Children:["Cu_t", "_Copy", "_Paste"] },
'    {Text: "_Help", Children:
'        [
'            {Text: "Checked Item", Selected: True, Shortcut: {Key: "F1"}},
'            {Text: "Disabled Item", Enabled: False},
'            {Text: "Item With Unique Event Name",
'                EventName: "mnu1234"},
'            {Text: "Item With Tag", Tag: "MyTag"}
'        ]
'    }
']
Private Sub MenuBar1_Action
    Dim mitem As MenuItem = Sender
    Select mitem.Text
        Case "_New"
            xui.MsgboxAsync(mitem.text & " selected.","menubar")
    End Select    
End Sub
Private Sub MenuBar1_SelectedChange (Selected As Boolean)
    Dim mitem As MenuItem = Sender
    Dim mhelp As Menu = MenuBar1.Menus.Get(2)
    Dim midisabled As MenuItem = mhelp.MenuItems.Get(1)        ' Disabled Item
    If Selected Then
        Log(mitem.Text & " is selected")
        midisabled.Enabled = True
    Else
        Log(mitem.Text & " is not selected")
        midisabled.Enabled = False
'        mhelp.MenuItems.Clear
        Dim m1 As MenuItem
        m1.Initialize("added Item", "m1")
        mhelp.MenuItems.Add(m1)
    End If
End Sub
 
Upvote 0

zed

Active Member
Licensed User
For the menus, you have this
Found in 2 seconds on the forum.
and
 
Upvote 0

omaroski

Member
Licensed User
Longtime User
Here's some test code with a default menu bar:
This helps but what if i have 2 items with check option?
If i add "Checked Item 2" it doesn't distinguish if i'm clicking "Checked Item" or "Checked Item 2":

JSON:
[
    {Text: "_File", Children:
        [
            "_New", "_Save",
            {Text: "_Export", Children: ["Zip File", "Tar File"]},
            "-",
            "_Close"
        ]
    },
    {Text: "_Edit", Children:["Cu_t", "_Copy", "_Paste"] },
    {Text: "_Help", Children:
        [
            {Text: "Checked Item", Selected: True, Shortcut: {Key: "F1"}},
            {Text: "Disabled Item", Enabled: False},
             {Text: "Checked Item 2", Selected: True},
            {Text: "Item With Unique Event Name", EventName: "mnu1234"},
            {Text: "Item With Tag", Tag: "MyTag"}
        ]
    }
]
 
Upvote 0

omaroski

Member
Licensed User
Longtime User
For the menus, you have this
Found in 2 seconds on the forum.
I've read that but still couldn't get my head around it.
 
  • Like
Reactions: zed
Upvote 0

omaroski

Member
Licensed User
Longtime User
Probably i figured it out. I need to add the event name to the JSON string so i can handle them separately

JSON:
{Text: "Checked Item", Selected: True, EventName: "chkItem"},
{Text: "Checked Item 2", Selected: True, EventName: "chkItem2"},

B4X:
Sub chkItem_SelectedChange (Selected As Boolean)
End Sub

Sub chkItem2_SelectedChange (Selected As Boolean)
End Sub
 
  • 100%
Reactions: zed
Upvote 0
Top