5

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?

3 Answers 3

2

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.

0
1

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();
1
  • Having some sort of Dictionary sounds also a good solution, and no need of extending the .NET controls, if I'm not wrong. Will take a look at extension methods, because it is something that I don't know about.
    – Ed.
    Commented Feb 15, 2012 at 13:40
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.