Hi there
I am experimenting with ExtenderProviders and run into a problem.
ExtenderProvider allows you to virtualy add properties to existing classes. My project uses Control as the base class. At DesignTime, I need a reference to Control.Parent for controls on a Form.
To illustrate, suppose you had the following trivial class ExtenderProvider (it doesn't do anything of interest except illustrate). This ExtenderProvider extends the Button class. I added two buttons to a Form, added the ExtenderProvider (which pops up on the toolbox after a previous build). The buttons have the TestString property as expected.
The issue is that, once the code goes into "EndInit", the Control.Parent of each button is still not set. InitializeComponent suggests that it should be set. And, at Run Time, it is set, but not Design Time.
Is this by design? Or am I doing something wrong?
The code forExtenderProvider, and for InitializeComponent (from my Form) is reproduced below:
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections.Generic;
namespace ExtenderProviderDemo
{
[ProvideProperty("TestString", typeof(Button))]
public class ExtenderProvider:Component, IExtenderProvider, ISupportInitialize
{
Dictionary<control,> testStrings = new Dictionary<control,string>();
public ExtenderProvider() { }
public string GetTestString (Control extendee) {
if(testStrings.ContainsKey(extendee))
return testStrings[extendee];
else
return string.Empty;
}
public void SetTestString(Control extendee, string testString) {
if (testStrings.ContainsKey(extendee)) {
testStrings[extendee] = testString;
}
else
testStrings.Add(extendee, testString);
}
#region IExtenderProvider Members
public bool CanExtend(object extendee) {
return extendee is Button;
}
#endregion
#region ISupportInitialize Members
public void BeginInit() {
}
public void EndInit() {
foreach (KeyValuePair<control,> pair in testStrings) {
MessageBox.Show("Control " + pair.Key.Name + " : Parent null ? " + (pair.Key.Parent == null).ToString());
}
}
#endregion
}
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.extenderProvider1 = new ExtenderProviderDemo.ExtenderProvider();
((System.ComponentModel.ISupportInitialize)(this.extenderProvider1)).BeginInit();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(102, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.extenderProvider1.SetTestString(this.button1, "");
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button2.Location = new System.Drawing.Point(115, 120);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.extenderProvider1.SetTestString(this.button2, "Test");
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.extenderProvider1)).EndInit();
this.ResumeLayout(false);
}