How to add for contour control

I am trying to add a checkbox for a loop, which when I enter 3, for example, in a text box, and click a button, it automatically adds 3 checkboxes in the form I tried this code, but added only one checkbox

private void button1_Click(object sender, EventArgs e) { int x = Convert.ToInt32(textBox1.Text); int m = 1; for (int i = 0; i < x; i++) { CheckBox button2 = new System.Windows.Forms.CheckBox(); button2.Location = new System.Drawing.Point(5, m); button2.Name = "button2 "+ m.ToString(); button2.Size = new System.Drawing.Size(51, 23); button2.TabIndex = m; //button2.UseVisualStyleBackColor = true; this.Controls.Add(button2); m++; } } 
+4
source share
2 answers

You set the location of all three buttons in almost the same place so that they appear on top of each other. Try to talk a little about it.

For example, change m++; on m += 40; .

+5
source

You need to touch the buttons a little. In addition, you must give each of your buttons a unique identifier.

 button2.ID = "Button_" + i; 
0
source

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


All Articles