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.
In order to inherit from a form, the file or namespace containing that form must have been built into an executable file or DLL. To build the project, choose Build from the Build menu. Also, a reference to the namespace must be added to the class inheriting the form.
Private member inheritance: A subclass does not inherit the private members of its parent class. However, if the superclass has properties(get and set methods) for accessing its private fields, then a subclass can inherit.
Create a Windows Form by using the Inheritance Picker. In Visual Studio, from the Project menu, choose Add Windows Form. The Add New Item dialog box opens. Search the Inherited Form template either from the searchbox or by clicking on the Windows Forms category, select it, and name it in the Name box.
Visual inheritance enables you to see the controls on the base form and to add new controls. In this walkthrough you will create a base form and compile it into a class library. You will import this class library into another project and create a new form that inherits from the base form.
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With