.NET inherited (WinForms) Form - VS designer issue

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.


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.