Eto: dynamically add and remove controls (EDIT)

@curtisw
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):
        self.Detach()

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.layout.AddRow(buttonAddGroup)
        self.layout.AddRow(None)

        self.Content = self.layout

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

    def ButtonAddGroupClick(self, sender, e):
        self.AddGroup()

def TestMyDialog():
    dialog = MyDialog()
    dialog.AddGroup()
    dialog.ShowModal()

if __name__ == '__main__':
    TestMyDialog()

Hey @dick.lelyveld,

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

Hope this helps!
Curtis.

1 Like

Hi Curtis,

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

Kind regards,
Dick

@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):
    try:
        self.Dispose(True)
    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?