Ed.
Ed.

Reputation: 876

Expanding functionality to .NET Controls

Currently I'm working in a tool that needs to create some sort of customized .NET with some additional fields. I've been wondering what approach is better to solve this, and been thinking in the following options:

Option A: - Create a derived class for each control (let's say, CLabel, CGroupBox...) where I define the new fields on each class. This would mean worse mantainability but would be easy to operate with.

Example:

class CLabel: Label
{
    public List<Control> RelatedControls {get; set;}
    public String textHint;
    // more stuff...

    public CLabel()
    {}
 }

Option B: - This option would mean not creating a derived class from each control, but using the actual Label, GroupBox, etc controls and creating a class encapsulating all the "extra" properties. This extra properties object would be referenced in the Control.Tag property. I have doubts about this one because referencing a sort of complex object inside the Tag property feels a bit crappy to me, but this would mean better mantainability and also of course no need of subclassing controls.

Example:

Label lbl = new Label();
lbl.Tag = new ControlDescription();

Option C: - This would mean having some sort of combination of Option A and B. Just creating the Custom Control, for example CLabel, that adds a type ControlDescription field to the Label control. This way we ensure encapsulation and mantainability, but we avoid the Tag thing.

I'm pretty sure there are lots of options out there better than those. Probably just using polymorphism - a concept I still have problems with - the custom control classes could be taken away. What of those options do you think is the best one? Do you think that all of this could be better done?

Upvotes: 5

Views: 266

Answers (3)

Alex
Alex

Reputation: 151

Probably you could try this:

public class ExtensionData
{
    //put your data here
}

public class Extended<T>
    where T : Control
{
    public Extended( T baseControl )
    {
        BaseControl = baseControl;
    }

    public T BaseControl { get; set; }

    public ExtensionData Extension { get; set; }
}

It's like your "option B", but without using "Tag" property

Upvotes: 0

Archeg
Archeg

Reputation: 8462

If I correctly understand you, I'd prefer to use extension methods here. If you need to store some custom values for each control - you can store them in the static dictionary (Control, Value) but use extension methods to access it, so it would look like you have controls with some values added.

Added an example:

public static class Extension
{
    private static Dictionary<Control, string> _controlStrings;

    public static void GetString(this Control ctrl)
    {
        return _controlStrings[ctrl];
    }
 }

After that you can use this method like:

Button btn = new Button();
string s = btn.GetString();

Upvotes: 1

d_schnell
d_schnell

Reputation: 614

If you need easy control of the properties at design-time and you must go with windows forms you should have a lock at this http://msdn.microsoft.com/en-us/library/system.componentmodel.iextenderprovider.aspx

This way you can provide properties to different controls without wrapping them first controls.

Upvotes: 2

Related Questions