B4J Question How to determine if a child menuitem from the menubar is checked = true in code

crt

Member
Licensed User
Longtime User
Hi, I'm trying to figure out how to tell if a menu item from the menubar is checked or not. I looked through the tutorials and did a forum search but can't seem to find the answer. Is there a way to test it in menubar1_action?

EDIT: Ok I answered my own question, I can do it With MenuBar1_SelectedChange (Selected As Boolean)

Though I still want to select it and unselect it programmatically. I read you need to add the menuitems to a map but I still don't know how to access the menuitem methods or properties. I hope someone can give me some insight.
 
Last edited:

crt

Member
Licensed User
Longtime User
Look at this post.

Instead of MenuItem, use CheckMenuItem.
Ok, I understand now, instead of Dim mi As MenuItem = MenuItems.Get(MenuTag) use Dim mi As CheckMenuItem = MenuItems.Get(MenuTag).
When I do that, the selected method shows up.
Thanks for your reply
So where do you find that in any of the documentation or tutorials? I searched and couldn't find any explanation for that.
 
Last edited:
Upvote 0

crt

Member
Licensed User
Longtime User
Menu.MenuItems can hold all kinds of menu items. All of them can be treated as MenuItem. However if you need to access a method or property that is not available in MenuItem then you need to cast it to the correct type.

The code you posted casts it to CheckMenuItem.
Thanks for your reply Erel, I did finally find that in the B4J beginners guide, though I didn't find any code examples on exactly how to do it. I guess I need a B4J for dummys, hopefully Wyken Seagrave will come out with something like that.
Thanks for giving us wannbe programmers great products.
 
Upvote 0

Patent

Member
Licensed User
Longtime User
1) assuming the menubar event name in the designer is "idmenu".
2) the Json string for the menuitems in the designer is:

[
{Text: "_Row1", Children:["_Load" ]},
{Text: "_Row2", Children:["_Save", {Text: "_Check", Selected: True}] }
]

then an easy solution is:

B4X:
Sub idmenu_Action

    Dim m As MenuItem=Sender
    Dim s As Object=Sender
    If GetType(s)="javafx.scene.control.CheckMenuItem" Then
        Dim c As CheckMenuItem
        c=Sender
    End If

    Select m.Text        
        Case "_Load"
           log(m.text)
        Case "_Save"
            log(m.text)
        Case "_Check"
            Log(c.Selected)
    End Select

End Sub


greets
 
Upvote 0

crt

Member
Licensed User
Longtime User
Yes, but you still need to use maps and tags to set the menuitems checked or unchecked on application start up. There is no other way I found to access those properties.
 
Upvote 0
Top