Retrieve MenuBar object from inside control event handler

Discuss the future of the AutoHotkey language
User avatar
kczx3
Posts: 634
Joined: 06 Oct 2015, 21:39

Retrieve MenuBar object from inside control event handler

Post by kczx3 » 27 Apr 2018, 11:18

I am trying to gain access to the MenuBar object of a GUI which is the parent of a button control. I am probably doing something wrong or misread/missed something in the documentation.

Code: Select all

#SingleInstance Force

myMenu := MenuBarCreate()
myMenu.Add("Hello", (ItemName, ItemPos, Menu) => MsgBox(ItemPos))
myMenu.Add()
myMenu.Add("World", (ItemName, ItemPos, Menu) => MsgBox(ItemPos))

main := GuiCreate("", "My Gui Title")
main.menuBar := myMenu
; main.MarginX := 0, main.MarginY := 0
main.OnEvent("Close", () => ExitApp())
main.Add("Button", "w200", "Click Me!").OnEvent("Click", "clicked")
main.Show()

clicked(ctrlObj) {
    MsgBox(ctrlObj.Gui.Title)
    MsgBox(IsObject(ctrlObj.Gui.MenuBar)) ; 0 when I would expect it to show 1
}
User avatar
Flipeador
Posts: 1018
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: Retrieve MenuBar object from inside control event handler

Post by Flipeador » 27 Apr 2018, 11:57

it seems like a bug...

Code: Select all

; from https://lexikos.github.io/v2/docs/objects/Gui.htm#MenuBar
FileMenu := MenuCreate()
FileMenu.Add "&Open`tCtrl+O", () => FileSelect()  ; See remarks below about Ctrl+O.
FileMenu.Add "E&xit", () => ExitApp()
HelpMenu := MenuCreate()
HelpMenu.Add "&About", () => MsgBox("Not implemented")
Menus := MenuBarCreate()
Menus.Add "&File", FileMenu  ; Attach the two submenus that were created above.
Menus.Add "&Help", HelpMenu
Gui := GuiCreate()
Gui.MenuBar := Menus
Gui.Show "w300 h200"

MsgBox Gui.MenuBar . "|" . Type(Gui.MenuBar) . "|" . IsObject(Gui.MenuBar)
ExitApp
Edit: Maybe the problem is here:

Code: Select all

		case P_MenuBar:
		{
			if (IS_INVOKE_SET)
			{
				ResultType result = SetMenu(*aParam[0]);
				if (result != OK)
					return result; // Already displayed error.
			}
			_o_return_empty; // TODO
		}
It seems like not implemented. :shock:
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
User avatar
kczx3
Posts: 634
Joined: 06 Oct 2015, 21:39

Re: Retrieve MenuBar object from inside control event handler

Post by kczx3 » 27 Apr 2018, 12:40

Lol! Could be!
lexikos
Posts: 6175
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Retrieve MenuBar object from inside control event handler

Post by lexikos » 04 May 2018, 21:38

I forgot to post here after releasing v2.0-a095 to let you know it's fixed, and it was also missing from the announcement topic. The fix was actually the first change after v2.0-a094.
User avatar
kczx3
Posts: 634
Joined: 06 Oct 2015, 21:39

Re: Retrieve MenuBar object from inside control event handler

Post by kczx3 » 07 May 2018, 07:12

Awesome, thanks! Apologies for not specifying which version I was using at the time of the OP. Will make sure I include that for any future posts.
Post Reply

Return to “AutoHotkey v2 Development”