How can I know which MenuItem or button was presse

2019-06-11 03:41发布

问题:

I want to create a dashboard to select (unhide and make active) one of the several sheets in my spreadsheet: I was thinking of a UInstance with a menu or several buttons.

It's seems difficult to create differents ServerHandler (and different CallBack functions) for each buttons or MenuItems, because their number is known only at run time.

It would be much simpler to use the EventInfo.parameter in the callback function, but this parameter is empty if I don't add a CallbackElement with an element that should have a setName() method.

The problem is that, to my big surprise, neither Buttons nor MenuItem do have the setName method!!!

Am I missing something very basic here? I doesn't seems logic that a menu item pressing event would contains a reference to the element that started the event itself?

Ok, maybe I should revert to a LIstBox which has the setName() method...

Thanks, Lauro

回答1:

The eventInfo contains the ID of the widget that calls the handler.

You can get it with e.parameter[source] in the handler function.(returns the ID as a string)

Just give a different ID to all of your buttons, one unique handler for all the buttons and you're done. (you can add the handler to the parent widget to simplify your code, for example if you use a grid to position the buttons add a single handler to the grid and it will work for all buttons)