Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How to add controls(components) to a form at design time?
 

How to add controls(components) to a form at design time?

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





汪汪已存圠 Monday, July 27, 2009 4:29 PM
Any idea?
汪汪已存圠 Tuesday, July 28, 2009 12:58 AM
You cannot use the code you would use in an application to add controls at runtime in the design-time environment. It won't work because, as you saw, the Designer will not serialize the instances. You need to call CreateInstance() from the designer host. See http://msdn.microsoft.com/en-us/library/yc1hyc2x%28VS.80%29.aspx. To remove labels, use DestroyComponent() (http://msdn.microsoft.com/en-us/library/system.componentmodel.design.idesignerhost.destroycomponent%28VS.80%29.aspx).
MCP
  • Proposed As Answer bywebJose Wednesday, September 23, 2009 4:54 AM
  •  
webJose  Friday, September 04, 2009 4:33 PM

You can use google to search for other answers

Custom Search

More Threads

• Please read before posting
• .NET Magic TabControl Help
• expandable property in property browser
• Generating BeginInit() & EndInit()
• Disable Control Designer for WinForm Control
• Scrolling in Integration.Elementhost
• AutoScaleMode Property does not exist
• Using interface as a property type?
• How designer "abstract" bug can be fixed for UserControls?
• Receiving Form Designer Error: "Type 'mpx.Controller' does not have a constructor with parameters of types DesignerHost."