Reputation: 1620
Let's say my application has about 300 standard controls in several forms. Some day it turns out it would be good to give all controls additional members.
What must be done? My thinking is:
Derive each type of Control
from a base class like this:
public partial class MyButton : Button, MyInterface
{
...
}
public partial class MyTextBox : TextBox, MyInterface
{
...
}
public interface MyInterface
{
// Additional members
...
}
This would mean touching every single Control
to change it from
private System.Windows.Forms.Button myButton;
private System.Windows.Forms.TextBox myTextBox;
to
private MyButton myButton;
private MyTextBox myTextBox;
and from
this.myButton = new System.Windows.Forms.Button();
this.myTextBox = new System.Windows.Forms.TextBox();
to
this.myButton = new MyButton();
this.myTextBox = new MyTextBox();
My question: Is there a simpler way? Maybe, if possible, replacing the Control
class by a class beeing additionally derived from MyInterface
? (Control.Tag
property is no option)
Upvotes: 1
Views: 308
Reputation: 125197
Creating extender providers seems to be a good option for you. A ToolTip
is an example; when you add a ToolTip
to a form, it adds a ToolTip on ToolTip1
string property to all controls.
An extender provider provides properties to other components. You can design your extender components to add some properties with different (simple or complex) types to different control types. The property provided by the extender provider actually resides in the extender provider object itself and therefore is not a true property of the component it modifies. At design time, the property appears in properties window. At run time, you can call the getter and setter methods on the extender component.
Resources
Example
Here is a really simple sample component which adds a string SomeProperty
property to TextBox
and Button
. The property is a simple one with no actual usage, but it's a start point for you:
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
[ProvideProperty("SomeProperty", typeof(Control))]
public class ControlExtender : Component, IExtenderProvider
{
private Hashtable somePropertyValues = new Hashtable();
public bool CanExtend(object extendee)
{
return (extendee is TextBox ||
extendee is Button);
}
public string GetSomeProperty(Control control)
{
if (somePropertyValues.ContainsKey(control))
return (string)somePropertyValues[control];
return null;
}
public void SetSomeProperty(Control control, string value)
{
if (string.IsNullOrEmpty(value))
somePropertyValues.Remove(control);
else
somePropertyValues[control] = value;
}
}
Upvotes: 3
Reputation: 41
If you only want to add methods, you could simply use extension methods.
Otherwise I would advise you to create Interfaces to define general/specific behavior, abstract classes for generic implementation of these interfaces (what inherits of classic controls) and finally to make your classes inheriting from these abstract classes. But as you mentionned it, you will have to rename it all. (You could imagine tricks with namespaces to avoid changing names or dynamically add members to a class but I would not advise it).
Upvotes: 1
Reputation: 567
I wouldn't do it. The normal working of the controls does not change, only want to store some extra information? I would use a global Dictionary, where MyExtension class contains all members would be added in your MyInterface.
Upvotes: 1