Not like VB6, but pretty easy to write code for yourself in C #.
If you create a control such as Button in the designer, you can copy the code from the *.Designer.cs file
It usually looks like
private System.Windows.Forms.Button button1; ... this.button1.Location = new System.Drawing.Point(40, 294); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 14; this.button1.Text = "Button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); ... this.Controls.Add(this.button1);
Cut this code and paste it instead, returning a button
private Button CreateButton() { private System.Windows.Forms.Button button1; this.button1.Location = new System.Drawing.Point(40, 294); // <-- change location for each this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 14; // <-- increase tab index or remove this line this.button1.Text = "Button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); this.Controls.Add(this.button1); return button; }
then call this method as follows
List<Button> buttons = new List<Button>(); for(int i = 0; i < 10; i++) { buttons.Add(CreateButton()); }
source share