Deriving C# form from base Delphi form

860 Views Asked by At

The software I am working on was originally written in Delphi, and any recent additions to the software have been done in C#. I would like to derive some C# forms from base Delphi forms. I have managed to get most of it working, except when I try to open the Design view of the C# form in Visual Studio 2008 I get an error. The error message is as follows "Constructor on type'base class' not found."

I have done some searching and apparently this is due to the Delphi form not having a default constructor which takes zero arguments. The Delphi TForm class does not have a default constructor which takes zero arguments so I can't just add one to my Delphi form.

The code I have compiles and runs exactly how I need it to, however I will need to be able to add new components to the C# form in the future. Anyone got any ideas on how I can get the Visual Studio design view working?

Here's a very simplified version of what I have:

Delphi Code:

type
  TMyDelphiForm = class(TForm)

  private
    ...
  public
    constructor Create(Sender : TObject);
  end;

implementation

  Constructor TMyDelphiForm.Create(Sender : TObject);
  begin
    inherited;
  end;

C# Code:

public partial class MyCSharpForm : TMyDelhpiForm
{
  public MyCSharpForm(Component sender) : base(sender);
  {
    InitializeComponent();
  }
}

Thanks in advance

4

There are 4 best solutions below

1
On BEST ANSWER

You cannot mix and match Win Forms and VCL Forms in the same application - period. They are two separate Windows frameworks and are not compatible. The only way to go is to redo the GUI in Visual Studio and link to your non-GUI components built as assemblies through the Delphi.NET compiler.

1
On

The designer requires a public parameterless constructor on the base class to work.

Dependant on the implementation of your base class, this might or might not be a show stopper. You may be able to solve the problem by adding something like:-

public MyCSharpForm() : base (null)
{
}

to your c# form. But you will need to ensure that the base class behaves properly at design time (ie, rendering logic can cope with the null, or whatever static value you provide for Sender.

6
On

I would add a base C# class that inherits from the TMyDelphiForm, and put all my Delphi / C# mapping code in there, in case I need to derive more than one C# form from the Delphi form, something like:

public class MyCSharpFormBase : TMyDelphiForm
{
    public MyCSharpFormBase() : base (null) { } 
}

and then inherit your form from MyCSharpFormBase. Haven't tried it, but I think it will satisfy the designer.

2
On

Do you have the source code for the delphi class? can you see what it does with the object/Component Sender parameter?

If you can instantiate something that will satisify the delphi class easily, then you can probably get designer support with an intermediate class that called the base like this:-

    public class MyIntermediateClass : MyDelphiForm
    {
      public MyIntermediateClass() : base(new Component())
      {
      }
    }

And then have your designable form inherit MyIntermediateClass