Show controls added programmatically in a WinForms application in the Design view?

Using Visual Studio (any version will do), is it possible for the controls to be added programmatically (rather than through the constructor view) so that they can then be displayed when returning to the Design view?

I tried something ridiculously simple:

public Form1() { AddCtrl(); InitializeComponent(); AddCtrl(); } private void AddCtrl() { this.SuspendLayout(); this.Controls.Add(new TextBox()); this.ResumeLayout(false); this.PerformLayout(); } 

... but to no avail.

Although I am sure this has been asked before, as this seems like a common question, I cannot find the answer!

+1
source share
1 answer

Does the designer execute my code?

When the form is displayed in the designer, the designer deserializes the code of your form (Form1.Designer.cs or the first class in Form1.cs) and creates an instance of the base class of your form and deserialize the InitializeComponent and create the controls that you declared in your class and set them properties.

Thus, codes in the Constructor will not be executed. The designer creates an instance of the base class of your form and does not look into the constructor of your form.

An interesting example of how a designer works.

Look at the code below and pay attention to the following problems:

  • Where ; s?
  • Constructor Form111111 for Form1 ?
  • What is NotDefinedFunction() ?
  • How int i = "xxxxxxxxxx" ?

Even if you create such a file, the designer will show correctly.

 using System using System.Collections.Generic using System.Drawing using System.Windows.Forms namespace Sample { public class Form1:Form { public Form111111() { NotDefinedFunction() InitializeComponent() } public void InitializeComponent() { int i = "xxxxxxxxxx" this.textBox1 = new System.Windows.Forms.TextBox() this.SuspendLayout() // // textBox1 // this.textBox1.Location = new System.Drawing.Point(0, 0) this.textBox1.Name = "textBox1" this.textBox1.Text = "text of text box 1"; // // Form1 // this.Controls.Add(this.textBox1) this.Name = "Form1" this.Text = "Form1" this.Size= new Size(250,100) this.ResumeLayout(false) this.PerformLayout() } private TextBox textBox1 } } 

And you will see the form in the designer:

enter image description here

How to add controls dynamically during development?

If you need such functionality, you can create your own dynamic controls in the base class constructor for your form, since the base class constructor will work when the child form is opened in the designer, then it will work during development.

But you should be aware that these controls are inherited and cannot be modified using the constructor of the child form.

So just create Form2:

 public Form2() { InitializeComponent(); AddDynamicControls(); } private void AddDynamicControls() { this.Controls.Add( new TextBox() { Name = "TextBox1", Text = "Dynamic", Location = new Point(100, 0) }); } 

Create a project, and then change the base class Form1 inherit from Form2 :

 public class Form1:Form2 

And the result will be:

enter image description here

Is there any other solution?

If you want to create some controls during development, I think you should take a look at T4 Text Templates .

You can use t4 templates to generate code during development. You have probably seen Entity Framework .tt template files. You can add a new Text Template element to your project and place the logic for generating elements during development in your t4 template.

+4
source

Source: https://habr.com/ru/post/1246160/


All Articles