10
votes

I have several forms in a C# application. I use Visual Studio 2010 Beta, but .NET 3.5 and C# 3.

I have a base form, called FilteredQueryViewForm in the Shd namespace and I want some other forms to inherit it (because they will basically do the same stuff, but with some additions).

I changed things from private to protected in the FilteredQueryViewForm class, so they're accessible from the derived forms. After this I've created a derived form and set the base class to FilteredQueryViewForm.

The designer of the derived class complained about Shd.FilteredQueryViewForm not having any constructors... regardless of the fact it had one, with 3 parameters. I thought parameters can be a problem, so I also created a (public, of course) constructor without parameters, but it still doesn't work. The error message is the same:

"Constructor on type 'Shd.FilteredQueryViewForm' not found."

And the designer of the derived class won't load. I have tried restarting vs2010beta, re-creating the derived form, but nothing seem to help. Google didn't yield any useful results for me on this problem. :(

Is this a problem of Visual Studio 2010 Beta? Or am I doing something wrong?

6

6 Answers

26
votes

You will need a constructor without parameters which calls the InitializeComponent() method in every of your forms. Then close the designer window, rebuild the solution and try to reopen the designer. That should work. Rebuilding the solution is essential.

The problem is, that if you create a form that inheritates from Shd.FilteredQueryViewForm, the designer will try to call the constructor of the parent form, but it loads this form not from code but from it's built assembly.

3
votes

I know that it's an old topic, but these things happen again, so I think that my contribute might be useful in future.

Emiswelt says "You will need a constructor without parameters which calls the InitializeComponent() method in every of your forms." This is not really needed. You can declare a custom parameterized constructor on the derived form and call normally "InitializeComponent" method (with a call to a custom contructor too). The important thing is that your constructor calls "InitializeComponent" (for new controls) and base constructor calls "InitializeComponent" (for inherited components). This situation will work at runtime, but you won't see inherited controls on Visual Studio designer. To show all the controls at design time you should only add a simple contructor without parameters in the base class.

For example, if your base is a form with a button and two radio buttons:

using System.Windows.Forms;
namespace Test
{
    public partial class Form1 : Form
    {
        public Form1(string foo)
        {
            //use "foo" here
            InitializeComponent(); //here button and radios will be initialized
        }
    }
}

You can see it on the design tool and you can avoid the blank constructor (with no parameters) without problems. The Form2 is now inherited from Form1:

namespace Test
{
    public partial class Form2 : Form1
    {
        public Form2(string foo) : base(foo)
        {
            //you can use "foo" here even if it is passed to base class too
            InitializeComponent();
        }
    }
}

There is no blank constructor and it will compile and run normally. At rutime your Form2 will show the same control set as Form1. But... you can't see it at design time because Visual Studio can't identify where "InitializeComponent" method is and an error is showed. Why? Because there should be a constructor without parameters somewhere on the calls' chain. The solution is a simple modification on the base class:

using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1(string foo):base()
        {
           //use foo here
        }

        public Form1()         //Visual studio designer likes this!
        {
            InitializeComponent();
        }
    }
}

That's all.

1
votes

I think you meant that your Form1.cs[design] didn't update when you added your base class. I had the same problem. Strangely enough, the program will run just fine when you press start, and you'll see your base class components on your Form when you run it, but not when you're in edit mode.

Just double click Form1.cs on the solution explorer. It worked for me. Do this

0
votes

Make sure the base form(s) are defined in an assembly that is compiled using the "AnyCPU" build option.

0
votes

I have tried all other answers, but in my case, the problem was in project properties. (I have experimented in default new WinForms project)

  1. Open project properties (project -> properties -> Build)Open properties
  2. Set "Platform target" to "Any CPU" Set platform
  3. Inherit "Form2" class from "Form1" class Inherit
  4. Rebuild project Rebuild

my project is on .NET Framework 4.7.1 target framework

That's all.

-4
votes

I had a similar issue with a different exception related to code in my base form's _Load method, so none of the solutions helped me. There was an exception in design-time that doesn't happen in run-time (null value referring to a static instance of another class). My solution was to throw a try-catch block around all of the code in that method.