Eto: dynamically add and remove controls (EDIT)

Is it possible to dyamically add and delete controls with Eto?
I want the user to be able to add a new group by clicking a button.

EDIT: so I tried several things and it seems it is possible. But…, I am able to add a control after initialization of the dialog (line 48), but when I click a button to add a control, it does not show it (line 44).
As far as I can debug, I do create a new group (IronPython.NewTypes.Eto.Forms.GroupBox_1$1 (2)) and a row (<Eto.Forms.DynamicRow object at 0x000000000000002C [Eto.Forms.DynamicRow]>), but adding it to the layout doesn’t seem to work…

Here’s my basic code. Any remarks are appreciated.

import Eto.Drawing as drawing
import Eto.Forms as forms

class Group(forms.GroupBox):

    def __init__(self, count):
        self.Text = 'Group {}'.format(count)
        self.Padding = drawing.Padding(5)

        buttonDeleteGroup = forms.Button(Text = 'Delete this group')
        buttonDeleteGroup.Click += self.ButtonDeleteGroupClick

        self.Content = buttonDeleteGroup

    def ButtonDeleteGroupClick(self, sender, e):

class MyDialog(forms.Dialog):

    def __init__(self):
        self.Title = 'Add and delete controls'
        self.ClientSize = drawing.Size(300, 500)
        self.Padding = drawing.Padding(10)
        self.Resizable = False

        self.countGroups = 0

        buttonAddGroup = forms.Button(Text = 'Add group')
        buttonAddGroup.Click += self.ButtonAddGroupClick

        self.layout = forms.DynamicLayout()

        self.Content = self.layout

    def AddGroup(self):
        self.countGroups += 1
        group = Group(self.countGroups)

    def ButtonAddGroupClick(self, sender, e):

def TestMyDialog():
    dialog = MyDialog()

if __name__ == '__main__':

Hey @dick.lelyveld,

To update a DynamicLayout after making changes, call DynamicLayout.Create().

Hope this helps!

1 Like

Hi Curtis,

I’ve searched several days for a solution, thank you very, very much!

Kind regards,

@curtisw, @dale one more question…

How would I go about removing a control? Now I use self.Detach(), but this does do what I want.
For when I create an extra 2nd group, remove the 1st group, and then add a 3rd group, the first group appears again… It’s not really deleted.

When trying to Dispose() the button on click (see below), Rhino creashes.

def ButtonRemoveGroupClick(self, sender, e):
    except Exception as ex:
        txt = 'Exception {0}, arguments:\n{1!r}'
        msg = txt.format(type(ex).__name__, ex.args)
        print msg
        return False

I think I should dispose of the group referring to it as a child from the parent? But how would I do that?