How to program a C# component in Visual Studio with "AutoList" capability?

Hi all, I’m finalizing a C# component in Visual Studio and I would like to add “AutoList” capabilities?
So that: when a ‘Value List component from Grasshopper’ is connected to the custom component, the list will get all possible values for that input. This is nicely illustrated on the following picture:


Can someone help me?
Kind regards

This is implemented in intrallattice on github

I had this in the old version of Peacock and and for the next version I’ve decided to change the focus. The problem with it being automatic is that it gives you automatic values, and it is possible that you already have a ValueList with certain values and if you plug it in it will change it. To avoid that I put a parameter between the value list and the component. Instead, what I’ll do now is to have a menu option in the parameter (by double clicking) to externalise a value list with default values. If you want the functionality for many cases I would recommend this other approach because it can be annoying to change the values once you have done it. But if you only want it for a particular case where it’s always good to have the same possible values, then that’s fine.

I can’t give you the whole code because it’s intertwined with several sides. I have a class to create default parameters that are created in each parameter as defined, one of them creates a ValueList, here source is the parameter in question. The base class is in charge of adding it to the document, plugging it to an input, saving an undo event and aligning it to the input. My base class for parameters has a menu option that does all this when the user claims it and then it expires the valuelist.

    public class DefaultValueList : DefaultSource<Grasshopper.Kernel.Special.GH_ValueList>
    {
        public DefaultValueList(string Nickname, List<string> Keys, List<string> Values, int DefaultIndexValue, Grasshopper.Kernel.Special.GH_ValueListMode Mode)
        {
            source = new Grasshopper.Kernel.Special.GH_ValueList();
            source.NickName = Nickname;
            source.ListItems.Clear();
            source.ClearData();
            for (int i = 0; i < Keys.Count; i++)
            {
                source.ListItems.Add(new Grasshopper.Kernel.Special.GH_ValueListItem(Keys[i], Values[i]));
            }
            source.ListMode = Mode;
            source.SelectItem(DefaultIndexValue);
        }
    }

If you want the automatic approach, in your component you have to check if your input contains a value list before been computed, maybe in BeforeSolveInstance(), and in that case clear the value list, add your values (with a code similar to the above one) and not sure if it should be expired, I guess yes but maybe this will do recompute your component two times. Not sure, I did that inside clusters an that make it different.

1 Like

Thank you for your well-written explanation! I have figured it out now! Thank you so much. I followed the approach by [Andrew Heumann] in (Generated ValueList not working)
In short: I’ve added a menu Item to the component which allows the user to click this extra option and it will create a pre-populated list of available options. This is perfect for my functionality. The buggy thing is you have to click twice, once on the the menu item and another click somewhere inside the GH document. I’m not sure if I can remove this. Best regards! Gieljan

You can also override AddedToDocument() and populate the ValueList when the component is added to the canvas. Just avoid add a new ValueList if that param has sources, to avoid repopulate twice when open the file.