Dynamic List Creation C#

Hey.

I’m trying to create a dynamic list component where one list is populated based on another selection. To start off with I need to have a c# component that can create and instantiate inputs on the grasshopper canvas. I’ve managed to do this before with panel components, however I’m having issues with a valuelist component and I have no idea why. The process I used to create the panel is the same as the one here, but it’s just not getting created on the canvas.

    protected override void SolveInstance(IGH_DataAccess DA)
        {
            // http://james-ramsden.com/grasshopper-automatically-create-a-value-list-in-c/

            bool buildIO = false;
            DA.GetData(0, ref buildIO);
            if (!buildIO) { return; }

            string rawStr = "";
            DA.GetData(1, ref rawStr);
            List<string> IOMapRaw = new List<string>(rawStr.Split(new[] { Environment.NewLine }, StringSplitOptions.None));
            List<List<string>> IOSplit = new List<List<string>>();
            List<List<string>> names = new List<List<string>>();
            List<List<string>> values = new List<List<string>>();
            IOMapRaw.ForEach(x => IOSplit.Add(new List<string>(x.Split('='))));
            IOSplit.ForEach(x => names.Add(new List<string>(x[0].Split(':'))));
            IOSplit.ForEach(x => values.Add(new List<string>(x[1].Split(':'))));

            var pManager = this.Params;
            var vlist1 = new Grasshopper.Kernel.Special.GH_ValueList();
            vlist1.CreateAttributes();
            vlist1.Name = "VList1";
            vlist1.NickName = "VList1";
            vlist1.Description = "Dynamically created list 1";
            vlist1.ListMode = Grasshopper.Kernel.Special.GH_ValueListMode.DropDown;

            vlist1.Attributes.Pivot = new PointF((float)this.Attributes.DocObject.Attributes.Bounds.Left - vlist1.Attributes.Bounds.Width - 30, (float)this.Params.Input[1].Attributes.Bounds.Y + 2 * 30);

            vlist1.ListItems.Clear();
            vlist1.SelectedItems.Clear();

            var vl1Vals = new List<string>();
            for (int i=0;i<names.Count; i++)
            {
                if (!vl1Vals.Contains(names[i][0]))
                {
                    vl1Vals.Add(names[i][0]);
                    var vl1Val = new Grasshopper.Kernel.Special.GH_ValueListItem(names[i][0], values[i][0]);
                    vlist1.ListItems.Add(vl1Val);
                }
            }
            var ghdoc = this.OnPingDocument();
            vlist1.SelectedItems.Add(vlist1.ListItems[0]);
            pManager.RegisterInputParam(vlist1);
            ghdoc.AddObject(vlist1, false);
            pManager.Input[2].Optional = true;
            pManager.Input[2].RemoveAllSources();
            pManager.Input[2].AddSource(vlist1);
        }

When I run the build button, it creates the valuelist, input source and attaches it to the c# block (it becomes an input on the c# component, can right click it and edit just like a regular valuelist panel) but just doesn’t create on the grasshopper canvas. Any help resolving this greatly appreciated.

(while I read this, you can encapsulate blocks of code using triple ticks ```. That means you can remove the excessive indentation and you get keyword colouring thrown in for free)

Hey David. Any update on this? I couldn’t manage to get what I wanted with it dynamically creating and populating the ValueList items on the canvas, but have got a workaround with a fixed 2 ValueList component that already has them attached to the Inputs. It just accesses the attached inputs and updates their GH_ValueListItems and triggers an ExpireSolution to update the item graphically (making sure to avoid infinite solution computation on updating an input to the component calling the Expire).