Reputation: 43
Suppose I want to develop a custom, purely graphical control to be used in both WinForms as well as ASP.NET. The control will, based on some property settings and associated rules, render an image.
Obviously, I could just duplicate the code from the WinForms version to the ASP.NET version, but that would mean that if an error is found, or a rule is changed, I have two versions to update.
I could write a core class, with the various properties and rules needed, and the WinForms and ASP.NET control classes will have a property pointing to this core class, something like:
public class MyControlCore
{
// all properties and control rules
public void PaintTo(Graphics graphics)
{
// rendering of control
}
}
public class MyWinFormsControl: Control
{
public MyWinFormsControl(Control parent): base(parent)
{
CoreControl = new MyControlCore();
}
public MyControlCore CoreControl { get; private set; }
protected override OnPaint(object sender, PaintEventArgs e)
{
CoreControl.PaintTo(e.Graphics);
}
}
This will work, but in the property grid I would get a property CoreControl
, containing the customization properties. Is there a way to have the property grid show the MyControlCore
properties, instead of the CoreControl
property with the sub properties?
I was thinking that I could perhaps accomplish this with TypeDescriptionProvider
and related classes, but I'm unsure how to proceed and if it would even give me the results I'm after. I realize this would only help me in the designer, but that's fine.
Upvotes: 4
Views: 254
Reputation: 10418
There is another choice. You could use partial classes and put the common code in a single file that you can link to from both projects.
Upvotes: 2