Custom Additional Menu Grasshopper Component

Hi,

I have a problem with custom additional menu option in the component.
The problem is this: I have boolean option, that I untick in the component. But once I copy the component the boolean state returns back do its default position, default position happens also if I reopen grasshopper file.

The code I have is this:

protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown menu) {
    base.AppendAdditionalComponentMenuItems(menu);

    var fooToggle = Menu_AppendItem(menu, "Min Triangulation", FooHandler, true, this.foo);
    fooToggle.ToolTipText = "Triangulation";
}

protected void FooHandler(object sender, EventArgs e) {
    this.foo = !this.foo;
    this.ExpireSolution(true);
}

And to explain the issue visually:

Saw this recently. Is about custom message but maybe helps:

https://www.grasshopper3d.com/m/discussion?id=2985220%3ATopic%3A1204259

1 Like

Tim is correct - for a component setting to persist through save/ reopen and copy/paste you need to manage that setting in overrides for the Read and Write methods. The link Tim posted should point you in the right direction.

Thanks it does solve the problem. I literally copy paste the code add changed the names:

    bool foo = true;


    public override bool Write(GH_IWriter writer) {
        writer.SetBoolean("Min Triangulation", this.foo);
        return base.Write(writer);
    }

    public override bool Read(GH_IReader reader) {
        this.foo = false;
        reader.TryGetBoolean("Min Triangulation", ref this.foo);
        return base.Read(reader);
    }

    protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown menu) {
        base.AppendAdditionalComponentMenuItems(menu);

        var fooToggle = Menu_AppendItem(menu, "Min Triangulation", FooHandler, true, this.foo);
        fooToggle.ToolTipText = "Triangulation";
    }

    protected void FooHandler(object sender, EventArgs e) {
        this.foo = !this.foo;
        this.ExpireSolution(true);
    }
2 Likes