-1

I would like to be able to create new windows forms that have some extra functionality built into them automatically via a 'middle man' derived class.

The extra functionality uses a bunch Win32 API calls that hook into some of the controls/events of the form and embed it into another window (like embedding a custom form into a notepad or excel window )

I created a derived class of the Form class with this extra functionality like so:

public class MT4ChartForm : Form

I didn't start a windows form project, I just added Form as the base class of my MT4ChartForm class

The idea here is if I ever needed to create another windows.form to be embeded elsewhere, I would create a new windows form project, which would set me up like:

public class Form1 : Form

And I would simply rename to:

public class Form1 : MT4ChartForm

Hoping to 'inject' MT4ChartForm's functionality into my new form automatically.

The problem is the visual form editor doesn't like it (drops an error screen) , and it probably causes other conflicts in the project settings :(

How can I do achieve what I am trying to do, which doesn't break the rules of C#?

Dale Woods
  • 784
  • 1
  • 13
  • 31
  • 3
    `drops an error screen` What does that screen show? – mjwills Mar 07 '18 at 12:42
  • Normally what you are trying to do works but there are some constraints. Most importantly MT4ChartForm cannot be just a class, it needs to have all the boilerplate code created by the designer – P. Kouvarakis Mar 07 '18 at 12:43
  • Possible duplicate of [.NET inherited (WinForms) Form - VS designer issue](https://stackoverflow.com/questions/1216940/net-inherited-winforms-form-vs-designer-issue) – Mark Benningfield Mar 07 '18 at 12:47
  • @P.Kouvarakis Thank you - do you have any resources on what the boilerplate code is? – Dale Woods Mar 07 '18 at 12:53
  • @MarkBenningfield Thank you, I will check it out. – Dale Woods Mar 07 '18 at 12:53
  • Create a new Form. Copy the code from the *.designer.cs file, preferably in a separate file, named MT4ChartForm.designer.cs. Provide only a default constructor in your class that calls `InitializeComponent()` – P. Kouvarakis Mar 07 '18 at 14:38

1 Answers1

0

The resources here helped.

I figured out, the designer needs the base class to have a constructor with no parameters.

    //constructor
    public MT4ChartForm(IntPtr in_hwnd) : base()
    {
        _hwnd = in_hwnd;            
    }

    //needed by form designer
    private MT4ChartForm() : base() { }

I created a private constructor to avoid confusion with derived classes, and the designer is happy with this.

Dale Woods
  • 784
  • 1
  • 13
  • 31