Which context menu button was click

Hello,

Anyone know how i can Figure out with context menu button was clicked?

if it helps the following is an example of a context menu button:

var mycontext = graphObjectMake(go.Adornment, “Vertical”,
graphObjectMake(“ContextMenuButton”,
{
width:220,
height:25
},
graphObjectMake(go.TextBlock,
{
text: “My menu button”
}),
{
click: function(e,obj)
{
//do something here to get the information on which button was clicked
}
}
)
);

Thanks!

Since you defined the GraphObject.click event handler on the “ContextMenuButton” Panel, the value of the second argument, obj, will be that context menu button.

Im probably just looking over it in the api but how do I get the text of the button?

That depends entirely on what you put inside the button Panel. The button might consist of lots of GraphObjects, including several TextBlocks. So just find the TextBlock and look at its text property.

But are you sure you want to determine the identity of a button by comparing with a user-visible string? What happens when it’s localized? If you don’t want to compare strings you could assign some data property on the button with some unique value (number or string or whatever). To avoid having GraphObject.make complain about your new property, prefix the property name with “_”.

As always thanks walter, that was perfect! I didnt know I could make new property names using “_” so this was helpful in multiple ways.

Here is an example of what walter meant by creating a custom “Property” for a button and the getting that ID when the button is clicked:

//find or write a function that generates a UUID, there are many available
var uniqueID = generateUUID();

var mycontext = graphObjectMake(go.Adornment, "Vertical", graphObjectMake("ContextMenuButton", { width:220, height:25,

_buttonID: uniqueID
},
graphObjectMake(go.TextBlock,
{
text: “My menu button”
}),
{
click: function(e,obj)
{
<span =“Apple-tab-span” style=“white-space:pre”> //On click this will get the ID for the button
var uniqueButtonID = obj._buttonID;
}
}
)

);

Although that feature is documented (GraphObject.make), we don’t encourage its usage because in JavaScript it’s possible to accidentally clobber one of the class’s internal data properties. Requiring the “_” prefix at least avoids the problem when you are using our minified go.js or go-debug.js libraries.