7

I have a Form named ScanFolder, and I need another Form, that needs to be very similar to ScanFolder, so I decided to use form inheritance. But there seems to be some misunderstanding with the constructor.

ScanFolder looks like:

public partial class ScanFolder : Form
{
    public ScanFolder(MainForm parent, bool[] autoModes, GlobalMethods GMethodsClass)
    {
        //Doing something with parameters
    }
}

I tried to inherit Form like this:

public partial class Arch2 : ScanFolder
{
}

But I get warning Constructor on type 'mhmm.ScanFolder' not found, and also there is an error on Arch2 Form edit mode, where I see a call stack error.

So I tried something like this:

public partial class Arch2 : ScanFolder
{
    public Arch2(MainForm parent, bool[] autoModes, GlobalMethods GMethodsClass)
        : base(parent, autoModes, GMethodsClass)
    {
    }
}

But it is still the same.

As you can see, I clearly don't have any idea what I'm doing. What I'm trying to achieve is getting Arch2 to look the same as ScanFolder, so I can see it in designer view and also override some methods or event handlers.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
andree
  • 3,084
  • 9
  • 34
  • 42
  • Even if you get this working, you can't display a form in the Designer if it does not have a default constructor (meaning one that does not accept any parameters). – Cody Gray - on strike Apr 20 '11 at 10:45
  • So if I get rid of all parameters and find any other way to use them in Form, I will be able to see my inherited form in design view? – andree Apr 20 '11 at 10:46
  • you can add another method in ScanFolder to init the params InitParams(MainForm parent, bool[] autoModes, GlobalMethods GMethodsClass) and leave the constructor empty to handle the GUI only – Gabriel Apr 20 '11 at 10:52
  • Related: *['UserControl' constructor with parameters in C#](http://stackoverflow.com/questions/1784303)* – Peter Mortensen Feb 17 '14 at 14:43

2 Answers2

14

To use the Forms designer, you will need to have a parameterless constructor:

public partial class ScanFolder : Form
{
    public ScanFolder()
    {
         InitializeComponent(); // added by VS
    }

    public ScanFolder(MainForm parent, bool[] autoModes, GlobalMethods gm)
       : this() // <-- important
    {
         // don't forget to call the parameterless ctor in each
         // of your ctor overloads
    }
}

Or, if you really need to have some init params, you can do it the other way around:

public partial class ScanFolder : Form
{
    public ScanFolder()
        : this(null, new bool[0], new GlobalMethods())
    {

    }

    public ScanFolder(MainForm parent, bool[] autoModes, GlobalMethods gm)
    {
        InitializeComponent(); // added by VS
        // other stuff
    }
}

I recommend the first approach, otherwise you need to pass some reasonable default parameters (I don't recommend passing a null parameter).

It seems that in some cases you will also have to restart Visual Studio after changing the class.

vgru
  • 49,838
  • 16
  • 120
  • 201
  • Sorry, but what do you mean by "call parameterless constructor"? Do i need to use ScanFolder() in each over overrided constructors in ScanFolder class? And what about inherited class? Should it look like my example in above post? – andree Apr 20 '11 at 11:03
  • +1. This worked. However, I had to ***restart Visual Studio*** in order for the designer to work (coming from a state where I didn't have a parameterless constructor) - rebuilding the solution, etc. was not enough. Perhaps this information could be added to the answer. – Peter Mortensen Feb 17 '14 at 08:54
1

You can use this code in parent form:

public partial class ScanFolder : Form
{
 public ScanFolder(MainForm parent, bool[] autoModes, GlobalMethods GMethodsClass)
  {
    //doing something with parameters
  }
}

and then in child form as:

public partial class ScanFolder : Form
{
  public ScanFolder(MainForm parent, bool[] autoModes, GlobalMethods GMethodsClass)
     : base(parent,autoModes,GMethodsClass)
  {
    //doing something with parameters
  }
}
bluish
  • 26,356
  • 27
  • 122
  • 180
Abbas Ghaf
  • 11
  • 1