Delete "unknown" menu

BlitzMax Forums/BlitzMax Programming/Delete "unknown" menu

danielos(Posted 2008) [#1]
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


Arowx(Posted 2008) [#2]
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


danielos(Posted 2008) [#3]
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


Arowx(Posted 2008) [#4]
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!


danielos(Posted 2008) [#5]
Thanks again :)


danielos(Posted 2008) [#6]
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


Arowx(Posted 2008) [#7]
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.



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


danielos(Posted 2008) [#8]
My mistake, didn't add "= New TList"
thanks a lot Merx!

Daniel