I add a stringproperty "AllLabels" to a class named "BaseForm",which inherite from System.Windows.Forms.Form,I want implement when I set the property "AllLabels" to "AAA;BBB;CCC" at design time(from the propertyGrid of the form),and there will be three new Labels which named "AAA","BBB","CCC" added to the form.
I had create an UITypeEditor bind to the property,override the EditValue method,in this method:
IDesignerHost host = (IDesignerHost)provider.GetService(typeof(IDesignerHost));
IContainer container = host.Container;
Label lbl = new Label();
lbl.Text = value.ToString();
container.Add(lbl);
return value;
I couldn't see any label in the from and the designer.cs code will be
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(465, 452);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Label label1;
but if I drag a Label from toolbox,the designer.cs will be
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(465, 452);
//this line is the difference
this.Controls.Add(this.label1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Label label1;
How can I implement this just by program just like I drag a component from the toolbox?
and when reset the property value to "AAA;BBB",the third label will be removed from form control collection.
please help me,thanks