Eto Add, Remove buttons dynamically

Hello
How to remove the new button created?

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

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.buttonAdd = forms.Button(Text = 'Add button')
        self.buttonAdd.Click += self.ButtonAddClick
        
        self.buttonRemove = forms.Button(Text = 'Remove button')
        self.buttonRemove.Click += self.ButtonRemoveClick
        
        self.layout = forms.DynamicLayout()
        self.layout.AddRow(self.buttonAdd, self.buttonRemove)
        self.layout.AddRow(None)
        self.Content = self.layout
        
        self.count = 0
        self.buttons = []

    def AddButton(self):
        self.count += 1
        self.button = forms.Button(Text = 'Add button'+str(self.count))
        self.layout.AddRow(self.button)
        self.buttons.append(self.button)
        self.layout.AddRow(None)
        self.layout.Create()
        
    def RemoveButton(self):
        self.count += 1
        self.layout.Remove(self.buttons[-1])
        #self.layout.AddRow(None)
        self.layout.Create()

    def ButtonAddClick(self, sender, e):
        self.AddButton()
    
    def ButtonRemoveClick(self, sender, e):
        self.RemoveButton()


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


if __name__ == '__main__':
    TestMyDialog()

You could try to hide it with “visible” property. It still will be there, but not accesible for user.

1 Like

This seems to work:

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

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.buttonAdd = forms.Button(Text = 'Add button')
        self.buttonAdd.Click += self.ButtonAddClick
        
        self.buttonRemove = forms.Button(Text = 'Remove button')
        self.buttonRemove.Click += self.ButtonRemoveClick
        
        self.layout = forms.DynamicLayout()
        self.layout.AddRow(self.buttonAdd, self.buttonRemove)
        self.layout.AddRow(None)
        self.Content = self.layout
        
        self.count = 0
        self.buttons = []

    def AddButton(self):
        self.count += 1
        self.button = forms.Button(Text = 'Add button'+str(self.count))
        self.layout.Clear()
        self.layout.AddRow(self.buttonAdd, self.buttonRemove)
        self.layout.AddRow(None)
        self.buttons.append(self.button)
        for b in self.buttons:
            self.layout.AddRow(b)
        self.layout.AddRow(None)
        self.layout.Create()
        
        
    def RemoveButton(self):
        
        if self.count>0:
            self.count -= 1
            self.buttons.pop()
        self.layout.Clear()
        self.layout.AddRow(self.buttonAdd, self.buttonRemove)
        self.layout.AddRow(None)
        for b in self.buttons:
            self.layout.AddRow(b)
        self.layout.AddRow(None)
        self.layout.Create()

    def ButtonAddClick(self, sender, e):
        self.AddButton()
    
    def ButtonRemoveClick(self, sender, e):
        self.RemoveButton()


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


if __name__ == '__main__':
    TestMyDialog()


1 Like

Thanks , i thought about it as alternative solution

Thank you , yes it work
I will try add more options to use the new buttons

    def resetLayout(self):
        self.layout.Clear()
        self.layout.AddRow(self.buttonAdd, self.buttonRemove)
        self.layout.AddRow(None)
        for b in self.buttons:
            self.layout.AddRow(b)
        self.layout.AddRow(None)
        self.layout.Create()
        
    def AddButton(self):
        self.count += 1
        self.button = forms.Button(Text = 'Add button'+str(self.count))
        self.buttons.append(self.button)
        self.resetLayout()

    def RemoveButton(self):
        if self.count>0:
            self.count -= 1
            self.buttons.pop()
        self.resetLayout()
1 Like