How to access inherited controls in the winforms designer - c#

I'm making some controls which all have to share the same look and some common behavior, although they are meant for different kind of inputs. So I made a BaseClass which inherit from UserControl, and all my controls inherit from BaseClass.
However, if i add controls for BaseClass in the designer, such as a TableLayoutPanel, i can't access them when I'm designing the inherited classes. I see the TableLayoutPanel, but even though he is "protected", i can't modify it or put controls in it through the designer. I've no trouble accesing it by code, but i don't want to lose the ability to use the designer.
Right now, i simply removed all controls from BaseClass, added the layout and all the common controls in each of the inherited class, then use references to manipulate them inside BaseClass. But that doesn't satisfy me at all. Is there a way to make the designer work with inherited protected member controls ?
Environment : C#, .NET 3.5, Visual Studio 2008
EDIT to answer SLaks's suggestion. I tried setting a property, and although I'm not used to use them it doesn't seem to work. Here is the code i tried :
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public TableLayoutPanel TableLayoutPanel1
{
get { return tableLayoutPanel1;}
set { tableLayoutPanel1 = value;}
}
}
public partial class UserControl2 : UserControl1
{
public UserControl2()
{
InitializeComponent();
}
}

When you try to access from the inherited control with the designer to the TableLayoutPanel declared in the base control, you're using a feature in WinForms called "Visual Inheritance".
Unfortunately TableLayoutPanel doesn't support visual inheritance:
http://msdn.microsoft.com/en-us/library/ms171689%28VS.80%29.aspx
That's why the TableLayoutPanel appears blocked in the inherited controls.

Try adding this attribute to the definition of the panel (this may or may not help):
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

You have to design the base controls on their own. Changes are reflected in the designer after you successfully rebuild the controls project. If you make the members public you can edit them but the changes won't persist.

Try making a ParentControlDesigner for your control, overriding InternalControlDesigner, and returning (designerHost.GetDesigner(tableLayoutPanel) as ControlDesigner). designerHost is (IDesignerHost) component.Site.GetService(typeof(IDesignerHost)).

I vaguely remember solving a similar problem by putting the base class it its own DLL and building it first. I've had a rummage but I can't find the project. Sorry.

Related

Generic control to have inherit methods in WinForm

I have to work on a project on which there are several Form which have 80% of the code the same. So I try to create a generic class to make inheritate all my Forms of the UserControl class (the basic one) and my own class. But .Net doesn't support multi classs inheritance. So I create a middle class to do the inheritance chain like I can see on the net but I think I miss another step. Each class is in a different file for information.
The problem is I can't open anymore the designer for my initial Forms, because "Visual Studio cannot open a designer for the file because the class within it does not inherit from a class that can be visually designed".
Other information, I have a Mainwindow which inherite from "Form" and call one or another UserControl I design to show it.
What I had at the beginning :
namespace i2SIMDCProduction
{
public partial class MyForm1 : UserControl
{
public MyForm1(MyOwnClass myClass)
{
InitializeComponent();
this.myClass = myClass;
}
}
}
namespace i2SIMDCProduction
{
public partial class MyForm2 : UserControl
{
public MyForm2(MyOwnClass myClass)
{
InitializeComponent();
this.myClass = myClass;
}
}
}
What I have now :
namespace i2SIMDCProduction
{
public partial class MyForm1 : MyMiddleClass
{
public MyForm1(MyOwnClass myClass)
{
InitializeComponent();
this.myClass = myClass;
}
}
}
namespace i2SIMDCProduction
{
public partial class MyForm2 : MyMiddleClass
{
public MyForm2(MyOwnClass myClass)
{
InitializeComponent();
this.myClass = myClass;
}
}
}
namespace i2SIMDCProduction
{
public partial class MyMiddleClass : UserControl
{
public void MethodForAllChild()
{
}
}
}
Thank you in advance for any kind of help. I tried different things already (create a third class at the top of the file of my Forms for example, create empty constructor, ...) but nothing which works for now. The more frustrating is it is compiling and working but only the designer is KO.
If you want different forms to share the same visual controls on the screen then you set up inheritance between the forms.
Use the inherited form option in Visual Studio
For example, Form1 has a group box, with a label and two text boxes
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void CommonMethod()
{
}
}
and Form2 inherits from From1 and adds a list box
public partial class Form2 : Form1
{
public Form2()
{
InitializeComponent();
}
public void SpecificMethod()
{
base.CommonMethod();
}
}
As you can see the controls from Form1 show up on Form2 also with a little icon to indicate that they are inherited.
If instead you just need to share code (like business logic) and not visual controls, then you create a separate class to hold the code with a link to the parent form, and then each form should contain an instance of this class.
What you want to do is a Model-View-Controler setup, where the Model is only data-related classes, View is only UI code, and the controller goes between the two doing the heavy lifting with processing user inputs and updating values.
The inheritance and designer in Windows Forms is a problem.
I have a Form with an splitter, two listboxes and some other controls. That form is used to translate (map) some items. You select one item at left, one at right and click button to match. They are the same item in different providers.
I have another provider that require some extra controls to do the translation. May be 90% or more of the code is the same, but I need some extra for this provider.
The options that I saw:
Add these extra controls (protected or public) to the Form, hidden by default and without use. In Form derived class, you use them. You haven't the designer in derived Form, but you don't need because controls are in base Form. The problem with this approach is that the designer part of inheritance of derived Form is in base Form. It's a nonsense. I don't recomend this option.
Don't use the designer in derived Form. Starting in the previous point, copy the designer code added for your derived Form into your derived Form and leave your base Form as at first stage, without nothing of derived Form. You don't use the designer but you can use it temporary, copy/paste and have a good inheritance... without the designer in derived Form. It's a good option if your derived Forms has few changes, few maintenance in the designer part.
You can "Add" some logic to your base Form to allow extensions. For example, below of the ListBox, I can add a Panel (hidden by default) and some methods like ShowLeftPanel/ShowRightPanel. By default, these panels aren't used, but in derived class I can add an UserControl in left panel and show it. And that UserControl show the properties that I need to show in the special provider. Add some virtual methods for listbox selection changed, to update the UserControl. In this way, your UserControl has designer and also the base Form. You only need add some "extension points" in your form (a Panel, a Splitter...) and give some methods to interact with this parts of the base Form. And this is ok with inheritance because is something generic, like Tag property in controls.
UPDATE
Check this solution and tell me about it. Make your Forms like this:
public partial class MyForm1 : UserControl, IMyUserControl
{
private readonly MyOwnClass myClass;
public MyForm1(MyOwnClass myClass, MyMiddleClass myMiddle)
{
InitializeComponent();
this.myClass = myClass;
this.MyMiddle = myMiddle;
}
public MyMiddleClass MyMiddle { get; }
}
In this way, all your panel's forms are IMyUserControl:
public class MyUserControl : IMyUserControl
{
public MyMiddleClass MyMiddle { get; }
}
So, having any of your panel's form, you can cast to IMyUserControl and get the related MyMiddleClass having access to methods like MethodForAllChild:
public class MyMiddleClass
{
public void MethodForAllChild()
{
}
}
In your main form, you may have some property or method that give you access to your UserControl. Create a method that give you the middle instance of the current UserControl:
private MyMiddleClass GetMyMiddle()
{
UserControl userControl = GetYourMainFormCurrentUserControl();
IMyUserControl myUserControl = userControl as IMyUserControl;
return myUserControl?.MyMiddle;
}
And use it in your main form when you need:
MyMiddleClass myMiddle = GetMyMiddle();
if (myMiddle != null)
{
myMiddle.MethodForAllChild();
}
In this way, you only need implement the interface and add a property in your forms/usercontrols. In the main form you can get this middleclass and the code to reuse is only in that class and shared in all places. You don't need copy/paste if you add or change something in the middle class.
UPDATE 2
I'm going to explain in other form how it works because the code is written above. The goal is having the code only in one place, without duplicate it.
You define an interface in a very similar way as a class but without implementation (this is not really true in lastest C# versions but we can suppose that is without code). C# don't allow multiple inheritance but you can derive from a class and implement as many interfaces as you want.
When we define IMyUserControl we are telling that every class that implements IMyUserControl, has a property MyMiddle. When MyForm1 implements IMyUserControl, if you don't add the MyMiddle property, you get a compiler error. The key with this solution is that add and implement this interface in each form is very easy: add IMyUserControl, the property and a parameter in the constructor to set the property.
So, all your forms implements now IMyUserControl. I don't know where are your forms but it's sure that you have a way to get access to your UserControl. Maybe a variable or an array in which you add your user controls. You are working with them, so you can access to your user controls. Well, if you have an UserControl instance, and you know that your UserControl implements IMyUserControl, you can cast your UserControl to IMyUserControl and after the cast, you have access to the interface, in this case, to the MyMiddle property.
And we put in MyMiddle all the code that you want to share.
If you add some code of your main form, where you work with your forms, I can help you with the code. I haven't more code than existing in my answer.

Visual Studio Form Designer Showing List of Controls

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.

How to expose ALL properties of a Control in a class derived from Form?

I have class derived from Form and it contains a TableLayoutPanel and in it one Label and one Panel. When I create instance of this Form, all properties and events of controls in design editor are read-only. Is there any way how to expose whole object for editing? I know that I can expose properties one by one, but that is not the best way in case when you want all of them.
Have a look here:
Avoid Visual Inheritance
The TableLayoutPanel control does not support visual inheritance in
the Windows Forms Designer. A TableLayoutPanel control in a
derived class appears as locked at design time.
You can use internal or make a getter method / property
public Label GetLabel() => return someLabel;
or
public Label MyLabel { get { return someLabel; } }
or
internal Label someLabel;

Showing public fields of a UserControl in designer

I am trying to add some fields to a custom UserControl that I am making. I have some fields that I like them to be visible in the Properties window of Visual Studio. I tried to use the flags below but I dont see the field in the designer, even after a compile.
How should I do this correctly?
public partial class TosChartControl: UserControl
{
#region PUBLIC FIELDS
[Browsable(true)] //Added this but still does not show up
[Category("Data")]
[DefaultValue(0)]
[Description("ID of the Sensor Node")]
public int NodeId { get; set; }
#endregion
public TosChartControl()
{
InitializeComponent();
}
}
I did clean and rebuild the soloution and projects but I cant still see this field in Properties window. Even restarting the Visualstudio didnt help.
UPDATE: Your public properties are visible in the designer only when it's in another control in the designer. It turns out that you don't need to add this attribute, properties are visible by default in the designer. As far as I understand, when it's in another component's design view, an instance of the user control is created and properties can be shown. Sorry for misleading you in the beginning, I thought it was necessary to add it.
Try this attribute:
[Browsable(true)]
http://msdn.microsoft.com/en-us/library/system.componentmodel.browsableattribute.aspx
To elaborate on henginy's updated answer:
Be sure that you are looking at an instance of the control you want to modify properties for, and not the definition of the control itself.
To clarify, when you add a property to your TosChartControl class, you won't see the property in the TosChartControl.cs [Design] tab, you will see it where you implement a TosChartControl, such as your Form1.cs [Design] tab, e.g. the containing control to which you have added your custom control.
...Assuming that your Properties window is visible, and that you have the control selected.
What to take away from this lesson:
Understanding what the properties window is actually showing you — It's contextual.
The difference between the model and the implementation of the model — e.g. Designing the custom control and designing the form that uses the custom control.

DataGridView locked on a inherited UserControl

I have a UserControl with some predefined controls (groupbox,button,datagridview) on it, these controls are marked as protected and the components variable is also marked as protected.
I then want to inherit from this base UserControl to another UserControl, however the DataGridView is always locked in the designer.
I suspect it may have something to do with the DataGridView implementing ISupportInitilize.
public class BaseGridDetail : UserControl
Has a DataGridView control (et al) defined.
public class InheritedDetail : BaseGridDetail
The DataGridView control is locked
Does anyone have any ideas how to make this control available in the designer after inheritenace?
By the looks of it, DataListView (and some other controls) do not support visual inheritance. There's a connect issue logged here which doesn't look hopeful.
There have been similar issues logged with other form controls, e.g. flowlayoutpanel.
I'm unable to find a way to force visual inheritance.
Here's the official answer on connect:
"For this particular release, the DataGridView was not designed to be used in visual intheritance. We will keep your suggestion in mind when we plan our future release"
That is of 26/05/2006.
Update: found this blog post which may have the answer
Edit: I was unable to verify the blog post's claims.
Looks like might be the latest on this issue
It looks like you can still manipulate the DataListView at runtime though, so you might be able to set visual properties (and other settings). It's not a great compromise.
[1] create Your Custom UserControl
[2] make your custom userControl use the below Inherited DataGridView:
[Designer(typeof System.Windows.Forms.Design.ControlDesigner))]
public class InheritedDataGridView : DataGridView { }
[3] Inherit from your Custom UserControl , And viola !!
[4] Ohh dont forget to add "System.Design" dll
Enjoy.
Add the datagrid to a panel control and set the modifiers of the panel and the datagrid to protected. This will all your inherited form design time access to the properties of the grid.
I left an answer but re-read your question and decided to delete it.
What is it about the DataGridView that you're trying to modify in the inherited control? It's columns? I've been able to do this by setting up a protected method in my base UserControl and passing the grid's column collection into it, like this:
// in base UserControl
public BaseGridDetail()
{
InitializeComponent();
InitGridColumns(dataGridView1.Columns);
}
protected virtual void InitGridColumns(DataGridViewColumnCollection columns)
{
columns.Clear();
}
Now your derived control can simply override that method, like so:
// in InheritedDetail
protected override void InitGridColumns(DataGridViewColumnCollection columns)
{
base.InitGridColumns(columns);
// add my own custom columns
}
Change property to defined [private] to [protected] defined at xx.designer.cs
which is originally machine generated code.
for example
private System.Windows.Forms.Button btnSave;
to
protected System.Windows.Forms.Button btnSave;
and rebuild it.
then you can change the property of inherited control.

Categories