Ed.
Ed.

Reputation: 876

Adding properties to a Control in C# .NET

I want to add some properties to some of the Controls inside the .NET framework. For example, I want to add a list of related controls - not the containing controls that already exists - to make some sort of link controls.

What I did is create a small interface decorator, implemented by a concrete decorator that extends from the class Control. Here I put the new properties and the methods to manage them.

The main problem is that when I create an instance of my decorated Control, I must pass as a parameter an instance of the base Control - let's say, Combobox -, and it is referenced in one of those new properties inside the decorator class.

When I try to paint that component, I'm unable to. And also not sure about why. The Control just doesn't show up.

Here is the decorator code:

    public class ControlDecorator : Control, IDecorator
    {
    private List<Control> RelatedControls = new List<Control>();
    private Control Control;

    public ControlDecorator(Control c)
    {
        this.Control = c;
        this.Control.MouseClick += new MouseEventHandler(Control_MouseClick);
    }

And here is how I create the Controls:

    Control lb = new Label();
    lb = new ControlDecorator(lb);
    editableArea.Controls.Add(lb);
    editableArea.Refresh();

Upvotes: 1

Views: 680

Answers (3)

j__m
j__m

Reputation: 9645

public ControlDecorator(Control c)
{
    this.Control = c;
    this.Controls.Add(c);
    this.Control.MouseClick += new MouseEventHandler(Control_MouseClick);
}

Upvotes: 0

Ravi Gadag
Ravi Gadag

Reputation: 15881

did you override the OnPaint method ?? as you have to Over-ride the method. you can change your field name to some descriptive name, rather than making ambiguous with Control Class. OnPaint

Control Class documentaiotn

Over riding on paint method

Upvotes: 1

Popescu Razvan
Popescu Razvan

Reputation: 159

You need to call InitializeComponent() method in ControlDecorator constructor.

public ControlDecorator(Control c)
{ 
  InitializeComponent();
  this.Control = c;
  this.Control.MouseClick += new MouseEventHandler(Control_MouseClick);
}

Upvotes: 0

Related Questions