Common code for WinForms and the ASP.NET custom control

Suppose I want to develop a custom, purely graphic control that will be used in both WinForms and ASP.NET. The control will, based on some property settings and related rules, make an image.

Obviously, I could just duplicate the code from the WinForms version to the ASP.NET version, but this will mean that if an error is detected or the rule changes, I have two versions for updating.

I could write a main class with various properties and rules, and the WinForms and ASP.NET control classes would have a property pointing to this main class, for example:

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 get a CoreControl property containing the configuration properties. Is there a way for the property grid to display MyControlCore properties instead of the CoreControl property with additional properties?

I thought I could do this with TypeDescriptionProvider and its related classes, but I'm not sure how to do it, and if that would give me the results I got. I understand that this will only help me in the designer, but it is wonderful.

+4
source share
1 answer

There is one more choice. You can use partial classes and put the common code in a single file that you can reference from both projects.

+2
source

All Articles