algorithmic modeling for Rhino
I am working on a component that takes in two lists, displays the the n-th index of the first list on the component itself as text, and outputs the n-th index of the second list as an object. Basically it's a "Value List" component that allows two inputs.
1. Is there any way to know which MenuItems index I am clicking? My code is the following:
As you can tell, the MenuItems are dynamically appended depending on what and how many items there are in my _firstList string list. The code: testString = _secondList[...] is simply pseudo-code.
2. Whenever I add/remove strings from my input list, AppendAdditionalMenuItems() gets called again, and again, adds all items again to the Menu. Demonstrated here:
Is there any way to remove/destroy previous items so it's not appended, but replaces the previous items?
Thanks!
Tags:
AppendAdditionalMenuItems() is called just before the menu is shown. You should then append the items you want to see and only the items you want to see. Never ever call AppendAdditionalMenuItems yourself. So all you have to do is clear _firstList in SolveInstance() and append all the new strings. Or did I miss something?
You can set the Tag field for each menu item, that how I usually keep track of which dynamic item is clicked. The Tag field is of type Object, but you can store an Int32 index in there no problem.
--
David Rutten
david@mcneel.com
No that bit is correct. But you should never have to remove items from a menu because each time the menu is shown it's a brand new menu. Old items don't 'stick around', unless your _firstList somehow isn't cleared.
When you call Menu_AppendItem, it will return an actual winforms menu item. You can then assign a tag to this item, as well as change the font, the tooltip text or any other property you want:
for (int i = 0; i < _firstList.Count; i++)
{
ToolstripMenuItem item = Menu_AppendItem(menu, _firstList[i], .....);
item.Tag = i;
}
Then, in your handler, cast the sender to a ToolstripMenuItem and harvest the Tag:
private void MyCustomItemClicked(object sender, eventargs e)
{
ToolstripMenuItem item = sender as ToolstripMenuItem;
int index = (int)item.Tag;
...
}
To use regular Grasshopper messages, use the AddRuntimeMessage() method.
--
David Rutten
david@mcneel.com
The 'as' and 'is' keywords in C# allow you to check whether an instance is of a specific type and also to cast the instance to that type. 'as' only works for reference types, which is why you cannot use it to convert the Tag to an int (int is a value type). You have to use the old fashioned (int) cast operator instead.
--
David Rutten
david@mcneel.com
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by