I have a problem with inheriting in Windows Forms.
I created a base form class with some controls and events and a class which inherits from the base class. When I try to open the designer of the inherited class it gives me this error message: "Duplicate component name .... Component names must be unique and case-insensitive".
So this error message appears because it copied some (not all) of the controls of the base form into the designer.cs of the inherited form. All of my controls are 'protected'.
Why it copies some of the controls in general and why not all?
Related
I know that getting the Form Designer to work is a ticklish business. Generics, x64, subtle problems with the project's XML... But perhaps someone can offer advice about my current problem, which is that a component I created that inherits from TabPage, when I try to view it in the designer shows up as a list of its controls, like this:
Thanks in advance.
You cannot make a TabPage as root of the designer, while you can do the same for a Panel or other container controls. The limitation is because, TabPage can only be hosted in TabControl, not even in the overlay control of the designer:
TabPage cannot be added to a
'System.Windows.Forms.Design.DesignerFrame+OverlayControl'. TabPages
can only be added to TabControls.
A control can be shown as root of the designer when the base class of the control has designer of type of DocumentDesigner. Form and UserControl are such controls which means when you create a new Form1:Form or new UserControl1:UserControl, since the base class derived from a designable control, then the class can be edited in the designer as root.
I believe you can handle your requirement by using UserControl, but for learning purpose (or as a workaround) if you want to make a control deriving from Panel designable, you can copy the following code in a code file:
public class MyControl: MyDesignableControl
{
}
[Designer(typeof(DocumentDesigner), typeof(IRootDesigner))]
public class MyDesignableControl : Panel
{
}
Then save it and then double click on it and you can see you can design it like a root control.
Then after you done with the design, change the Panel to TabPage.
Remarks on
DocumentDesigner
This designer is a root designer, meaning that it provides the
root-level design mode view for the associated document when it is
viewed in design mode.
You can associate a designer with a type using a
DesignerAttribute.
For an overview of customizing design time behavior, see Extending
Design-Time
Support.
In my app namespace = DRT, I'm creating control classes (e.g., button, textbox) that derive fron their corresponding Windows control classes, e.g.,
internal abstract class DRT_Button_Abstract : Button
{
....
}
internal class DRT_Button_CancelSearch : DRT_Button_Abstract
{
....
}
internal class DRT_Button_StartSearch : DRT_Button_Abstract
{
....
}
All together I currently have 13 derived classes that derive either from one of my abstracts or from a Windows control class. After a successful build, I see my control classes (e.g., DRT_Button_CancelSearch and DRT_Button_StartSearch) on the Toolbox and I successfully drop them onto my main form. All is fine for a while, but ultimately, I'll go to open the main form.cs [Design] (i.e., the UI designer) and it will show the error The variable '{control property name}' is either undeclared or was never assigned. for some combination of my controls.
When I examine the main form Designer.cs file, the expected code for all the controls is present EXCEPT for the expected new statement. They are not present in the main form Designer.cs file. For example, I expect to see this.drt_Button_CancelSearch = new DRT.DRT_Button_CancelSearch(); but its missing
I've tried ignoring the error, proceeding to the UI designer windows to re-apply the lost controls, but the problem just repeats with the newly applied control
What the heck is going on? Is there a way to recover from this situation?
This is most likely a problem of the Designer not being able to clear/reload its cache. There is not much you can do. In the past I:
closed and reopened all designers that have user controls
put all the controls in a separate project (in the same solution)
put all the controls in a separate solution/Visual Studio instance and set a proper reference to the controls' dll (or even nuget package)
With the first two options I have had varying success. Reopening the designer is not very convenient and doesn't work.
That last option is the best but also the most annoying because every adjustment requires a rebuild of the project and update of the reference/package.
Also make sure that all controls that you create have public default constructors and function well when this constructor is used.
it's any way to inherit form from baseForm, f.e:
i have Baseform with menu and some button. Now I want to use it in my second form, but i would not copy-paste, but only:
public partial class Form1 : BaseForm
and now i have some problems, because compilator send me bugs:
Message 1 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:
dziedziczony --- The base class '_10widokow.BaseForm' could not be
loaded. Ensure the assembly has been referenced and that all projects
have been built.
Those errors probably due to your BaseForm either not being referenced, or you have other problems outside of the scope of the question.
Controls on a form are added by the InitialiazeComponents method generated by the Form graphical editor. You don't need to inherit to bring in a basic set of controls, but simply copy the generated code out to a common location.
Then call in the constructor of the forms in which you want the base controls.
I added a form control to my form in a designer. But I need to override that control's WndProc. Can I do that without creating a new control extending the old one? Because when I extend the old one like this my designer won't work anymore:
partial class ThatControlWithWndProc : TheControlIActuallyWant {}
Or how can I get my designer to work with this new control that I created and not throw me an error?
If you just edit the .Designer.cs file directly to refer to your overridden control, and ensure that it follows the rules for controls to be designer-compatible (like having a default constructor, and not relying on any other initialisation) you should be fine.
How do I create a WinForms User Control that has no GUI or designer attachment? An example is like the Timer Control, which you drop onto your form and it docks to the bottom but doesn't have any GUI controls?
You need to use System.ComponentModel.Component as base class.
Example :
class Class1 : System.ComponentModel.Component
{
}
Create a class that inherits from System.Windows.Forms.Control. This should change the icon from the normal "class" icon to a "component" icon in the solution explorer. You can see this effect if creating a class and inheriting from say TextBox or Timer