Constructor of custom form with pythonnet

701 Views Asked by At

I have a problem with the custom constructor of a form(created in C#) which extend XtraForm from DevExpress.v17.1 library. It has two constructors:

protected BaseForm()
{
    InitializeComponent();
}

and

protected BaseForm(IClient client)
{
    InitializeComponent();
    ... many code
}

Where IClient is interface. This form has many dependencies and all of them compiled in the library. When I extend this form and try to create the instance by code:

class TestApp(BaseForm):

def __init__(self):
    self.Text = "Hello World From Python"
    self.components = System.ComponentModel.Container()
    self.AutoScaleBaseSize = Size(5, 13)
    self.ClientSize = Size(392, 117)
    h = WinForms.SystemInformation.CaptionHeight
    self.MinimumSize = Size(392, (117 + h))

    # Create the button
    self.button = WinForms.Button()
    self.button.Location = Point(160, 64)
    self.button.Size = Size(150, 20)
    self.button.TabIndex = 2
    self.button.Text = "Click Me!"

    # Register the event handler
    self.button.Click += self.button_Click

    # Create the text box
    self.textbox = WinForms.TextBox()
    self.textbox.Text = "Hello World"
    self.textbox.TabIndex = 1
    self.textbox.Size = Size(126, 40)
    self.textbox.Location = Point(160, 24)

    # Add the controls to the form
    self.AcceptButton = self.button
    self.Controls.Add(self.button)
    self.Controls.Add(self.textbox)

def button_Click(self, sender, args):
    """Button click event handler"""
    print ("Click")
    WinForms.MessageBox.Show("Please do not press this button again.")

def run(self):
    WinForms.Application.Run(self)

def Dispose(self):
    self.components.Dispose()
    WinForms.Form.Dispose(self)

Run init code:

def main():
    form = TestApp()
    form.run()
    form.Dispose()

if __name__ == '__main__':
    main()

I have an error:

Traceback (most recent call last):
  File "C:/Users/v.khvorostianyi/PycharmProjects/CSharp/Test.py", line 141, in <module> 
    main()
  File "C:/Users/v.khvorostianyi/PycharmProjects/CSharp/Test.py", line 85, in main
    form = TestApp()
TypeError: no constructor matches given arguments

Python=3.6.2, pythonnet=2.3.0
.NET=4.6.1

Project need for automated test, this form necessary for with work process. Why I have such error?

1

There are 1 best solutions below

0
On BEST ANSWER

The constructors in your BaseForm is hidden by protected access modifier and is accessible only within BaseForm and its derived class instances. So, form = TestApp() cannot be used, because the constructor with empty arguments is hidden.

There are at least two ways to solve this:

0. You can use public access modifier in your BaseForm constructor.

public BaseForm()
{
    InitializeComponent();
}

public BaseForm(IClient client)
{
    InitializeComponent();
    //... many code
}

1. You can try to overload the .net constructor by using __new__ method in your derived class:

def __new__(cls):        
    return BaseForm.__new__(cls)