Writing a Very Simple Value Selector in VisualStudio

Hi All, I have searched up and down and can’t seem to find the source code for a very, very simple Value List component in C#. All I’m trying to do is give a predefined list of 5 possible single letter string values and let the user pick just 1 to feed into a component. I have found more sophisticated examples, such as @andheum’s from Human UI (and thanks for sharing the source code, Andrew!). But since I’m just starting out with these kinds of objects, maybe others would benefit from seeing a minimal example, as well.

Thanks in advance!

As an alternative, Param_Integer allows you to specify named values like this:

protected override void RegisterInputParams(GH_InputParamManager pManager)
{
    pManager.AddIntegerParameter("Season", "S",
        "Spring = 0\nSummer = 1\nAutumn = 2\nWinter = 3", GH_ParamAccess.item, 0);
    var param = (Param_Integer)pManager[0];
    param.AddNamedValue("Spring", 0);
    param.AddNamedValue("Summer", 1);
    param.AddNamedValue("Autumn", 2);
    param.AddNamedValue("Winter", 3);
}


NamedValue.gha (5.5 KB)
NamedValue.zip (22.0 KB) (Source)

6 Likes

Ah, excellent to know. Thank you very much, Mahdiyar!
That worked perfectly when I used the integers to generate the right string.

I’d still love to see the code for the drop-down Value List, if you or anyone else runs across it has the basic form at hand (see one that’s just the gh component with the values added below).

But this is capable of what I need at the moment. Thanks again!

Do you have to create a new component from scratch? In Human UI I automatically generate a Grasshopper native value list and pre-populate it with the values I want, see here: https://github.com/andrewheumann/humanui/blob/master/HumanUI/HumanUI/Components/UI%20Main/SetWindowProperties_Component.cs#L172

1 Like

Hi Andrew, Thanks for your response. I actually don’t have to create one from scratch at all, as the component it feeds just takes a text string, plus it returns a default value and warning if you don’t give it one of the five allowable letters.

@Mahdiyar’s example was good (and learned how to add values to the component dropdown menu with it), and inputting these values into the Value List component from the GH core libraries is fine too.

My interest in finding a minimal example is to start having a better understanding of things like UI elements into which I haven’t delved at all yet. But it hurts literally nothing if I don’t write one custom. Thanks again for your time, and for all you’ve done for the Grasshopper community!

Writing custom UI for components is a lot of work.A lot of the utility classes/methods utilized by the native components are private, so you can’t rely on them for your own components, and end up having to re-write huge portions of the code from scratch. I like to try to extend built-in Attributes classes where possible, and if I have to build one up from scratch I usually give up :joy:

2 Likes

Hi Andrew, thanks for the heads-up. This tells me I should feel 100% okay about my current setup.
I may loop back around and implement James Ramsden’s Value List generator script later. But since world-class Grasshopper component authors don’t wade into those waters unless they have to, sounds like I’m okay for now.

Thanks again!