I have the following custom Form (MyFrm) which inherits from Form.
public class MyFrm<T>: Form where T: class
{
}
And following is my Form1:
public partial class Form1: MyFrm<CONTACTS_BASE>
{
public Form1()
{
InitializeComponent();
MyInitialize();
}
public void MyInitialize()
{
}
}
as can be seen, there is nothing exceptional, However, when right click and select view designer I get the following error:
The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file:
Form1 --- The base class 'MyGym.Controls.MyFrm' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.
when I remove the part below and edit my Form1 accordingly I get no errors when I go to the designer mode.
: Form where T: class
Why am I facing this issue? is there a fix for this?
Thanks
I believe you need to explicitly provide the InitializeComponent() method so that the Visual Studio IDE (Designer) works properly.
public class MyFrm<T> : Form
where T : class
{
public MyFrm() : base()
{
InitializeComponent();
}
private void InitializeComponent()
{
}
}
and then chain the constructors it together
public partial class Form1 : MyFrm<CONTACTS_BASE>
{
public Form1() : base()
{
InitializeComponent();
MyInitialize();
}
public void MyInitialize()
{
}
}
Note that : base was added to the constructor. However, in this example it's a bit overkill as the base constructor would already be called implicitly. I provided this addition, due to this answer. It states you must keep the constructor parameter-less in your base class.
Related
I have a Visual Studio 2010 Windows Forms app which includes a Form base class that other classes will inherit. The base class' constructor takes a parameter that the child classes will pass to the base class.
Example:
public partial class BaseForm : Form
{
public BaseForm(int number)
{
InitializeComponent();
}
}
public partial class ChildForm : BaseForm
{
public ChildForm(int number)
: base(number)
{
InitializeComponent();
}
}
The problem that I'm running into is, when I attempt to open the ChildForm in VisualStudio's Design View mode, I receive the following error:
Constructor on type 'MyProject.BaseForm' not found.
Note: regardless of the error, the project compiles and runs fine.
I can avoid the error if I overload the constructor with one that does not contain any parameters.
Example: (This gets rid of the error)
public partial class BaseForm : Form
{
public BaseForm(int number)
{
InitializeComponent();
}
public BaseForm()
{
InitializeComponent();
}
}
public partial class ChildForm : BaseForm
{
public ChildForm(int number)
: base(number)
{
InitializeComponent();
}
}
My question is, how can I create a base class that does not include a parameterless constructor and avoid the Design View error?
That is completely impossible.
The form you see in the design view is an actual instance of your base class.
If there is not default constructor, the designer cannot create that instance.
You can mark the constructor with the [Obsolete("Designer only", true)], and make it throw an exception if called when not in the designer, to prevent other people from calling it.
You need to adjust your BaseForm output type, In the properties for the project, change the Output type from Windows Application to Class Library.
ref:
https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/walkthrough-demonstrating-visual-inheritance
Problem: When I execute my code it pulls up the code correctly from the inherited base class and changes the text of the base form correctly. But as soon as I, in debugger mode in VS2013, come back into the derived class and step over the InitizlieComponent(); call everything is reverted back. When I say 'reverted back' I mean that thing go back to the way that they were in the designer for the derived form.
All help would be appreciated!
Derived Class
public partial class appSuiteMenu : App.baseForm
{
public appSuiteMenu()
: base("App Suite")
{
InitializeComponent();
}
}
Base Class
public partial class baseForm : Form
{
public baseForm() { InitializeComponent(); } //Required or designer breaks
public baseForm(string formTitle=null)
{
this.initialize(formTitle);
}
private void initialize(string formTitle)
{
base.text = formTitle + " - " + //Other objects are called from the class
}
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
}
My guess would be the second call to the "InitializeComponent()" inside the derived class.
What is does, is exactly what you said. It overrwrites everything from the base initialization.
I believe you can remove it and it'll be fine:
public appSuiteMenu()
: base("App Suite")
{
}
Read more about it in here: http://www.dotnetperls.com/initializecomponent
Edit:
Here is some tests I did with two forms. One is derived from the other. The base form hase a label. The second from adds another label. When creating a form of the derived type, tou get a form with two labels. What I did was use InitializeComponent(); in both base and derived forms.
The code:
public partial class BaseForm : Form
{
public BaseForm()
{
InitializeComponent();
label1.Text = "asdf";
}
}
...
public partial class DerivedForm : BaseForm
{
public DerivedForm()
{
InitializeComponent();
label2.Text = "asdfasfd";
}
}
What happens in execution of the code is that the base initialization happens. Then the derived initialization adds only the add functionality.
It seems pretty straightforward, but Iv'e noticed you had only one InitializeComponent(); in the code above that will actually happen.
Dave,
Your edited posted is nearly what I ended up doing to correct my problem. Since I was overriding the .Text() property of the base form, I found that I didn't even have to pass anything in the constructor. Simply setting the form text in the code, or designer, changed everything for me.
I have a C# Windows Forms Application form1.cs with a Class Library (DLL) called class1.cs. Now on the UI side I do the following:
using System;
...
using System.Windows.Forms;
using ClassLibrary1;
namespace UI
{
public partial class Form1 : Form
{
MyLibraryClass mlc = null;
public Form1()
{
InitializeComponent();
mlc = new MyLibraryClass(this);
}
public void aMethod() {
Console.Write("Test");
}
}
}
In the Class Library I take the Form reference and want to call the method within, but I don't have access to it:
...
using System.Windows.Forms;
namespace ClassLibrary1
{
public class MyLibraryClass
{
private Form _form;
public MyLibraryClass(Form form)
{
this._form = form;
this._form.aMethod(); //Not working!
}
}
}
The reason as I understand it is that my ClassLibrary1 only knows Form but not Form1 and hence cannot call methods from Form1. The problem is, the UI knows the Class Library but not the other way around, since that would create a ring dependency as you certainly know. But how can I solve this problem?
Instead depeding of Form you can create an interface.
public interface IMyInterface {
void aMethod();
}
Form1 will implement the interface we created
public partial class Form1 : Form, IMyInterface
{
MyLibraryClass mlc = null;
public Form1()
{
InitializeComponent();
mlc = new MyLibraryClass(this);
}
public void aMethod() {
Console.Write("Test");
}
}
In MyLibraryClass now you will depend on the interface not the form. This way MyLibraryClass can use any form that respect the contract and we make sure that in MyClassLibrary will never be passed any intruder form.
public class MyLibraryClass
{
private IMyInterface _form;
public MyLibraryClass(IMyInterface form)
{
this._form = form;
this._form.aMethod(); // now is work :)
}
}
Notes:
The interface will be created in Class Library project (where MyClassLibrary is created).
I recommend you to take a look on SOLID principles.
Circular dependencies are something you will want to avoid whenever possible, but let's assume you still need this.
The easiest solution would be to put the MyLibraryClass and Form1 in the namespace, so you can replace the Form type parameter with a Form1. If you want to keep things nicely separate however, you will have to add a new type.
In you library you would have to add an interface that contains that method.
public interface IMyInterface
{
void aMethod();
}
If you then change the parameter to an IMyInterface instead, you have access to that method. If you need access to normal Form methods or the aMethod is always the same, you could opt for an abstract class that inherits from Form too.
Depend upon Abstractions. Do not depend upon concretions
public interface IMethod
{
void aMethod();
}
public partial class Form1 : Form,IMethod
public MyLibraryClass(IMethod form)
{
this._form = form;
this._form.aMethod();
}
If you can, you should enforce statically the Form1 class in the argument list:
public MyLibraryClass(Form1 form)
{
// ...
}
If you cannot (which is often the case when several assemblies are used) you should test and cast dynamically:
public MyLibraryClass(Form form)
{
if (form is Form1)
{
(form as Form1).aMethod();
}
}
BUT YOU REALLY SHOULD honor the DIP (Dependency inversion principle), instead: depend from abstractions. Implement an interface and depend from that instead of Form1.
But if you've had the issue in the first place, you probably haven't cleared out for yourself how dependencies are organized between assemblies or, worse, don't know exactly why some answers propose to use interfaces.
Read the link above to know more about why, because your problem really is not about HOW but WHY.
This question already has answers here:
How can I get Visual Studio 2008 Windows Forms designer to render a Form that implements an abstract base class?
(10 answers)
Closed 8 years ago.
I have designed 3 different win forms in c#.
public partial class SLOSPR : form
{
public bool IsCallable;
public void PopulateData();
}
public partial class SLOFIX : form
{
public bool IsCallable;
public void PopulateData();
}
public partial class SPDBID : form
{
public bool IsCallable;
public void PopulateData();
}
I have declared one member variable IsCallable in every form's class and a member function PopulateData() in every form's class. It is so because, in a code segment I want to declare one variable of a form type, and want to assign an object of any of the above 3 classes and use that IsCallable variable and call PopulateData().
like this:
public form RetForm()
{
form frm=new /* constructor of any 3 forms mentioned above*/
//this function can return any of the three types which will be decided at run time.
return frm;
}
form frm=RetForm();
here i want to use IsCallable and call PopulateData().
like this:
if(frm.Iscallable)
frm.PopulateData();
Which is not possible syntactically as Iscallable and PopulateData is not member of form class
To solve this problem I extended the form class to an abstract class and declared those 3 forms from the extended class.
public abstract class EXTENDED_FORM : form
{
public bool IsCallable;
public abstract void PopulateData();
}
and changed the definition of those 3 forms like this.
public partial class SLOSPR : EXTENDED_FORM
{
public override void PopulateData()
{
/*body for SLOSPR */
}
}
public partial class SLOFIX : EXTENDED_FORM
{
public override void PopulateData()
{
/*body for SLOFIX */
}
}
public partial class SPDBID : EXTENDED_FORM
{
public override void PopulateData()
{
/*body for SPDBID*/
}
}
Now I did like this:
public EXTENDED_FORM RetForm()
{
EXTENDED_FORM frm=new /* constructor of any 3 forms mentioned above*/
//this function can return any of the three types which will be decided at run time.
return frm;
}
EXTENDED_FORM frm=RetForm();
Now I can call like this:
if(frm.Iscallable)
frm.PopulateData();
Automatically overridden PopulateData will be called.
Finally I made my purpose. But as I changed
public partial class SLOSPR : form /*[Designer generated code]*/
to
public partial class SLOSPR : EXTENDED_FORM
The GUI designer in Visual Studio gets messed up, showing the following page.
How to get rid of this, also my purpose gets fulfilled without hampering the designer???
don't make EXTENDED_FORM abstract; remove abstract from its declaration and add protected constructor without parameters
Here's my wish to create something abstract in WPF. You've got a main window (called main for example, even if it's not correct we don't care) with two buttons. Those two buttons have the same function : they open a new window, the same for both of them, but with different things inside. So I decided to create an abstract class to rule them like that :
public abstract (partial ?) class A : Window
{
public A()
{
InitializeComponent(); // Not sure about that, it's kinda weird to use it here no ?
}
...
}
public partial class B : A
{
public B()
{
InitializeComponent(); // Since it's already in A I shouldn't have to use it here right ?
}
...
}
public partial class C : A
{
public C()
{
InitializeComponent(); // Same thing here...
}
...
}
Debugging gives me something like : "error CS0263: Partial declarations of 'namespace.B' must not specify different base classes".
Removing 'partial' from A class gives : "error CS0260: Missing partial modifier on declaration of type 'namespace.A'; another partial declaration of this type exists".
I know that 'partial' specifies if a class has another part of her somewhere else (like the xaml file beside the cs one), so I guess the abstract class has to be partial too since it contains my controls. Maybe the B class shouldn't be partial ?
I know (memories) it works with Windows Forms, but there's no xaml files so it's easier, and I didn't find any useful tips. I think I understood that this problem occurs when I don't change something in my xaml file, which doesn't works as simply as "class : abstract class". Maybe the subclass thing ?
Please note that I'm a beginner in WPF apps, so I thank you in advance if your answer is as detailed as possible.
Thanks !
You need to define your base class all in code without using XAML :
Code for class A :
public abstract class A : Window { }
Code for class B :
public partial class B : A
{
public B()
{
InitializeComponent();
}
}
xaml for class B :
<yourNamespace:A x:Class="yourNamespace.B"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:yourNamespace="clr-namespace:yourNamespace"...
A C# partial class is one that is defined over two or more source files. The other 'part' of Window is generated by Visual Studio, and includes things like InitializeComponent()
If you want a base Window class, then it has to be defined entirely in code, and have no XAML component. It will then no longer be a partial class.
public abstract class WindowA : Window
{
// define base methods here
}
Then you can derive WindowB from WindowA:
public partial class WindowB : WindowA
{
public WindowB()
{
InitializeComponent();
}
}
But you also need to do it in the other 'part', i.e. in XAML, so in WindowB's XAML file, the root Window tag needs to be changed to:
<wpfApp:WindowA x:Class="WpfApp.WindowB"
wpfApp:WindowA ="clr-namespace:WpfApp"
(you'll need to change the namespace appropriately)
This will generate the other 'part' deriving from WindowA, so there will be no inconsistency.
The InitializeComponent() method should be called in each derived class' constructor, as it's specific to that class, i.e. in this case, the other 'part' is generated from the XAML, and defines an InitializeComponent() with resourceLocator code that specific to WindowB.