Delete "unknown" menu

BlitzMax Forums/BlitzMax Programming/Delete "unknown" menu

Hello!

My program creates a unknown number of menus...
When one of these menus is clicked,
GadgetText(TGadget(Eventsource())) delivers the denomination of the menu, so far it is not too difficult
But there is a problem when one of these menus should be deleted...
How can this be done ?

Greetz,
Daniel

I think the problem you are having is because of the way you are creating the menu items without storing a reference to them...

Generally whenever you create somthing this gives you the option to store a reference to the created object in a List or Map data structure.

e.g.
menuItem = CreateMenu(...)
menuList.addLast(menuItem)

or
menuItem = CreateMenu(...)
menuId = menuParent.GetText()+"->"+menuItem.GetText()
menuMap.Insert(menuId,menuItem)

Once you have a list or map of the menus then you can simply search through it for the menu you need to delete.

Hope this helps
Merx

good idea, thank you Merx...
I want to use

menuItem = CreateMenu(...)
menuList.addLast(menuItem)

So how can I delete a menu by menutext with lists ?
I have very little experience with lists ^^

Cheers,
Daniel

With a list you will do something like this...

For local menuItem:TGadget = eachin menuList
If menuItem.GetText() = targetMenuText then
... do stuff on target menu item
exit 'get out of the loop
endif
Next

Simply loop though the items in the list using a for eachin loop, checking the text of the menu item matches the one your looking for.

NB: Maps are better as they allow you to look up the item based on a "key" and the key can be the name of the the menu item. So for a map it would be...

menuItem = menuMap.ValueForKey(targetMenuName)

NB: example code is pseudo code only.. off the top of my head!

Thanks again :)

But theres a another problem:

menuItem = CreateMenu(...)
menuList.addLast(menuItem)

does not work... ("Error - Null Object")
I guess the problem is that
addLast(obj)
needs an Object input, but the menu handle is TGadget...
How can this be fixed ?

Greetz,
Daniel

Hi Daniel, I've just tried it with the CreateMenu example and it worked fine as long as I used the CreateMenu(...) with parameters in brackets function?

menuItem should be defined as TGadget, I think all objects should be extensions of the type object e.g.

' Example setup a gui menu then delete a menu item

Framework MaxGui.Drivers
Import BRL.Event
Import BRL.EventQueue

SuperStrict 

Local menuList:TList = New TList

Local window:TGadget
Local filemenu:TGadget
Local editmenu:TGadget
Local helpmenu:TGadget

Const MENU_ABOUT:Int=109
Const MENU_DELETE_ME:Int=110

window=CreateWindow("My Window",40,40,320,240)

filemenu=CreateMenu("&File",0,WindowMenu(window))

menuList.AddLast(filemenu)

Local menuItem:TGadget

menuItem = CreateMenu("&About",MENU_ABOUT,filemenu,KEY_N,MODIFIER_COMMAND)
menuList.AddLast(menuItem)

menuItem = CreateMenu("&DeleteMe!",MENU_DELETE_ME,filemenu,KEY_W,MODIFIER_COMMAND)
menuList.AddLast(menuItem)

UpdateWindowMenu window

While True
	WaitEvent 
	Select EventID()
		Case EVENT_WINDOWCLOSE
			End
		Case EVENT_MENUACTION
			Select EventData()
				Case MENU_ABOUT
					Notify "Arowx~n(C)2008 Amazing Software"
				Case MENU_DELETE_ME
					For Local menuItem:TGadget = EachIn menuList
						If menuItem.GetText() = "&DeleteMe!" Then
							Notify "Found it"
							FreeGadget(menuItem)
							UpdateWindowMenu(window)
							Exit
						EndIf
					Next
			End Select
	End Select
Wend



The Null Object would indicate that the the menu item failed to create, did you provide it with valid parameters e.g. parent ect?

Regards
Merx

My mistake, didn't add "= New TList"
thanks a lot Merx!

Daniel