3

I have a problem in deriving a form called PrinterForm This is the code of the parent form:

public partial class PrinterForm : Form
{
    public PrinterForm()
    {
        InitializeComponent();
    }

    private void PrinterForm_Load(object sender, EventArgs e)
    {
        LoadSomething();
    }

    private void LoadSomething()
    {
        //Return a List<dynamic> of Dapper (query work fine!). The function is already used elsewhere so no problem here
        var list = new DapperRepository().GetAllOfSomething(); 
    }
}

And this is the code of the children form:

public partial class FormChildren : PrinterForm
{
    public FormChildren()
    {
        InitializeComponent();
    }
}

When I try to access the FormChildren designers an error is reported and is not shown. The error that is reported to me is the following:

System.Windows.Forms.Design.IEventHandlerService

Trying to comment on the LoadSomething function can correctly display the designer.

What's the problem?

Lorenzo Belfanti
  • 1,205
  • 3
  • 25
  • 51
  • Is it happening when you run the application OR in visual studio designer? – Kamalesh Wankhede Nov 23 '16 at 11:55
  • @krw12572 Visual Studio Designer – Lorenzo Belfanti Nov 23 '16 at 11:58
  • From your code of `FormChildren` class, it seems like you are either accessing InitializeComponent method in parent class OR you have created another WindowsForm(`FormChildren`) and are trying to inherit `PrinterForm`. Both options seems incorrect. If you want to inherit a form then just create a simple class and inherit already created `PrinterForm`. Then see if your problem persists or not. – Kamalesh Wankhede Nov 23 '16 at 12:02
  • @krw12572 I tried to create a simple class, as you described, and derive the father. Same result! – Lorenzo Belfanti Nov 23 '16 at 12:34
  • How are you creating the FormChildren using the IDE? Adding a new Form or adding a new CLASS? – JohnG Nov 23 '16 at 12:39
  • @JohnG Already tried both cases, same result. IT DOES NOT WORK! ƃɐʍs – Lorenzo Belfanti Nov 23 '16 at 12:48
  • @LorenzoBelfanti, Did you build your solution after changing the code? – Kamalesh Wankhede Nov 23 '16 at 12:49
  • @krw12572 Of course. Also tried to clean up and rebuild. ǝıɥƃuıɯ – Lorenzo Belfanti Nov 23 '16 at 12:51
  • 1
    @LorenzoBelfanti The constructor of base form will be executed when you open an inherited form in designer. In fact the designer creates an instance of base form and put controls on it. So for example Load event of base form which attached in constructor of base form, will be executed. To learn more about how the designer works, take a look at this post: [Can't view designer when coding a form in C#](http://stackoverflow.com/a/32299687/3110834). It also contains an interesting example. You will find it useful. – Reza Aghaei Nov 23 '16 at 17:38

3 Answers3

1

I think you're problem is in the LoadSomething function, you should try to put a condition so it is not executed when you're working with the designer.

See : Error when opening designer on inherited form

Community
  • 1
  • 1
SilentRage47
  • 934
  • 2
  • 14
  • 31
0

I had a same problem. I solved my problem by add base() to child form constructor.

public partial class FormChildren : ParentForm
{
    public FormChildren() : base()
    {
        InitializeComponent();
    }
}
eliasetm
  • 1,399
  • 11
  • 20
0

This can happen when you change the namespace from brackets to the new shorthand form namespace xxx; in any of your inherited forms form.cs file.

For example, this works:

Namespace xxx { public partial class InheritedForm:Form { yourcode ... } }

This does not work:

namespace xxx; public partial class InheritedForm:Form { yourcode ... }

Also, you must have a constructor with no parameters that Initializes the components:

public BaseForm()
{
    InitializeComponent();
}
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129