Hello,
I am having a hard time understanding how to position properly an automatically added control (a value list in my case) relative to its parent component.
What I would like to do is load one or many predefined value list when I put my component on the canevas for the first time.
How would you achieve a constant margin (say 30px) between the left part of a component and the right part of a value list ?
public override void AddedToDocument(GH_Document document)
{
if (Params.Input[3].SourceCount == 0)
{
// Perform Layout to get actual positionning of the component on the canevas
this.Attributes.ExpireLayout();
this.Attributes.PerformLayout();
//instantiate new value list
var control = new Grasshopper.Kernel.Special.GH_ValueList();
control.CreateAttributes();
control.NickName = "Load Direction";
//populate value list with our own data
control.ListItems.Clear();
control.ListItems.Add(new Grasshopper.Kernel.Special.GH_ValueListItem("X", "0"));
control.ListItems.Add(new Grasshopper.Kernel.Special.GH_ValueListItem("Y", "1"));
control.ListItems.Add(new Grasshopper.Kernel.Special.GH_ValueListItem("Z", "2"));
control.ListItems.Add(new Grasshopper.Kernel.Special.GH_ValueListItem("XX", "3"));
control.ListItems.Add(new Grasshopper.Kernel.Special.GH_ValueListItem("YY", "4"));
control.ListItems.Add(new Grasshopper.Kernel.Special.GH_ValueListItem("ZZ", "5"));
control.ToggleItem(2);
// get the correct bounds of the populated value list
control.Attributes.ExpireLayout();
control.Attributes.PerformLayout();
//customise value list position
var x = (float)this.Attributes.Bounds.Left - control.Attributes.Bounds.Width / 2 - 30;
var y = (float)Params.Input[3].Attributes.Pivot.Y - control.Attributes.Bounds.Height / 2; // 11 is half the height of the control
control.Attributes.Pivot = new PointF(x, y);
// add to doc and connect
document.AddObject(control, false);
Params.Input[3].AddSource(control);
}
base.AddedToDocument(document);
}