I have inherited user controls in the following manner:
ControlBase
|__ControlInherited1
|__ControlInherited2
ControlBase contains methods and properties that are overridden in each inherited control, but it has no other controls at all. Inherited controls override the base methods and have sub-controls on them and totally different from each other.
I have the ControlBase added to the form, and I want to change it to either ControlInherited1 or ControlInherited2 depending on run-time conditions.
I'd like to take advantage of the inheritance so I do not have to add different routines for every inherited class
I tried the simple controlBase = new ControlInherited1(); line, but the objects of ControlInherited1 control is not displayed on the form.
Is such visual inheritance even possible?
The problem you have is that even if your new control is inherited from the base control, there's no automatic way of applying the existing properties to the new one you're adding with the call controlBase = new ControlInherited1();. In essence, you'll lose all information about the original control when overwrite the original variable by creating a new instance of the ControlInherited1 class and setting it as the value of controlBase.
The correct way to replace an existing control would be to first remove the existing control and then adding the other one to the form, like following:
this.Controls.Remove(controlBase);
this.Controls.Add(newControl);
On top of this, you'll have to do some manual work to make sure all the properties that you want to persist from the original control are applied to the new (replaced) one.
Related
Setup:
I have created a Form that I wish to have serve as the base from which I will inherit other forms. This base form serves as a "template" of sorts, but it also provides a good deal of functionality related to the structure, as well as the interrelation of all of the controls provided.
A primer for the images that follow... The top info-colored bar is a custom control inherited from ToolStrip. The bottom strip is another custom, again inherited from ToolStrip. The left white block is a TreeView and the right block is a TabControl (having deleted all TabPages from it...I intend for these to be added in the inherited forms).
Image of base form in designer:
Image of inherited form in designer:
Clearly, the only difference is that when I open the inherited form, I get a little box icon superimposed over each control, and when I click them, I get the padlock telling me I cannot edit.
The problems:
All controls on the inherited form are locked. I have researched the issue of visual inheritance, and as far as I can tell, I'm not using any controls that expressly do not support it, as this link suggests there are. In this Q&A, Hans suggests changing the modifier on those controls, which I have done. In fact, I tried both Public and Protected, all to no good result.
I am stumped.
This is a technical restriction in the designer, it is specific to the SplitContainer control you are using. And some other ones. The trouble-maker is the ISupportInitialize interface.
Controls use this interface when they can't afford the properties of the control to be assigned in an arbitrary order. The designer helps when it sees that the control implements this interface, it calls the BeginInit() method when it starts assigning properties, EndInit() when it is done. The control uses these methods to delay the side-effect of property assignments, the EndInit() method makes them effective. Important for SplitContainer, the minimum sizes of the panels also affect the splitter position.
Perhaps you can see the rub, the InitializeComponent() method in the base form class has already called ISupportInitialize.EndInit(). So modifying properties again in the derived form class is unlikely to turn out well. The designer protects the control from this by locking it.
Very inconvenient, there is no simple workaround. If modifying the SplitContainer in the derived form class is a hard requirement then you'll have to give up on inheriting it or write the code by hand in the derived class constructor.
I'm using a custom dynamic class object in a PropertyGrid (too much code to post here). In a nutshell, what it does is allow me to create properties at run-time from arbitrary input (in this case XML but that's beside the point). These properties may be any type, including another custom class so they can be nested indefinitely.
The issue I'm having is that the PropertyGrid sees this class as a Collection, and has the button in the Value column that allows you to open the CollectionEditor. This (among other things) has Add and Remove buttons, both of which will fundamentally break the custom class. Rather than try and make these two complicated systems work together nicely, I'd rather simply disable the CollectionEditor altogether. I have another system which allows you to see the child objects of the class (by setting the PropertyGrid.SelectedObject to the child object) and it works fine. I'd prefer the user not even having the option of seeing the CollectionEditor, as it seems to be not at all compatible with this system.
Ideally, I'd like to just put a button in the Value column that I could capture a click-event and handle myself, but that's optional. I also don't care if I disable the CollectionEditor for just the row(s) in question or for the whole PropertyGrid. I'm not using it either way, so anything which just makes it not accessible would be good.
Have you tried deriving your own CollectionEditor and in there override some methods/events to make its form not appear and just return without doing anything?
You then need to mark your collection type properties with:
[EditorAttribute(typeof(MyCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
This will automatically launch your MyCollectionEditor when clicking on the "..." button for the marked collection property in the PropertyGrid.
You may also need to inherit PropertyDescriptor and override the GetEditor method to return your own CollectionEditor.
I have created a custom control that inherits System.Windows.Forms.Panel, and adds a few extra properties. I then intend to create child classes that inherit this custom-panel class, and add content to them.
The custom-panel class will be passed to a "Wizard" framework (with back/next buttons) as the content for the various steps. I intend to make extensive use of this, creating 40-50 different pages for Wizards to handle various things in my project.
Question: Is there a way to view just the panel in the Designer, and modify its layout and design from there? I could code everything the hard way, but I really don't want to.
I did some searching and found this article, but that discusses creating a custom control and adding it to a library. I don't need to do this, I just want to view/edit the control in Designer directly, without adding it to a Form.
Obvious Answer to the rescue again.
Create a custom control, add the layout/split panel as desired, and change it's property to DockStyle.Fill.
This makes your custom control "behave" like the layout control, as long as you add all other controls to the layout control.
add first this name space
using System.ComponentModel.Design;
Apply the System.ComponentModel.DesignerAttribute attribute to the control as follows:
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public class UserControl1 : System.Windows.Forms.UserControl
{
...
}
now you can edit your custom user control in designer environment
Maybe its something stupid, but I'm having a problem with a subclass of a DataGridView Control in VS2005 C#. I know I can subclass from almost anything by doing
public class MyDataGridView : DataGridView
{}
no problem, and I put in some things / elements I want applicable globally. Now, I take this gridview and put into a custom user control that will contain other controls too. So I have something like created by the visual designer. I grab some buttons, label, and my derived "MyDataGridView" on it.
public partial class MyCompoundDGVPlus : UserControl
So, now, I can visually draw, move, change all sorts of settings as needed, no problem.
Now, I want this "MyCompoundDGVPlus" class as the basis for other classes, of which I will manipulate settings specific, but want all to have the same look / feel, and otherwise similar flow, hence the derivations.
I've even set the "modifiers" setting to public, so I SHOULD be able to modify any of the properties of the controls at any derived level. So, now, I create a new subclass of "MyFirstDetailedDGVPlus" derived from "MyCompoundDGVPlus". Ok visually, all the label, button, datagridview appear. However, now I want to specifically define the columns of the datagridview here in this class visually, but its locked. However, the LABEL on the form, I CAN get all the property settings....
What am I missing.
Maybe you should take a look at this post as it seems to do what you are looking for :
DataGridView locked on a inherited UserControl
I've come across the feature in Visual studio to auto-generate a subclass of a custom control using Add New Inherited User Control.
But I haven't found a clear description on how to e.g create a subclass of Button for instance. Apart from the actual way to do it, I'm also interested if VS provides helpful code-generation for this?
You just create your own class that inherits the Control, that you would like to subclass. For instance:
class BetterButton : Button { ...}
That is the easy part. Now you have the option to override various methods or properties, depending on what you want to achieve with your new Control. It could be anything, really. One thing I often see used is overriding OnPaint to get the control drawn in a custom way; and still getting the behaviour of the original control.
In terms of UserControls, I often see that a "parent" UserControl contains some UI logic and basic UI elements, while the subclassed controls are refinements of the parent for specific use.