Creating Controls Dynamically in C#
Dynamically Generated Controls the user selects the number of radio buttons to generate and then presses the Build Itt button
The Complete Source Code private void btnBuildIt_Click(object sender, EventArgs e) { int hsep = 40; int num = (int)numUDCount.Value; if (numUDCount.Value > 0) Form1.ActiveForm.Height = 115 + num * hsep; GroupBox gBox = new GroupBox(); gBox.Text = "Demo Group"; gBox.Size = new Size(200, 20 + num * hsep); gBox.Location = new Point(20, 20); this.Controls.Add(gBox); for (int i = 0; i < num; i++) rb.Add(new RadioButton()); rb[i].AutoSize = true; rb[i].Location = new Point(15, 20 + hsep * i); rb[i].Name = "radioButton" + Convert.ToString(i); rb[i].Text = "This is Radio Button " + Convert.ToString(i); gBox.Controls.Add(rb[i]); } btnBuildIt.Enabled = false;
num is the number of Radio Buttons to be created private void btnBuildIt_Click(object sender, EventArgs e) { int hsep = 40; int num = (int)numUDCount.Value; if (numUDCount.Value > 0) Form1.ActiveForm.Height = 115 + num * hsep; GroupBox gBox = new GroupBox(); gBox.Text = "Demo Group"; gBox.Size = new Size(200, 20 + num * hsep); gBox.Location = new Point(20, 20); this.Controls.Add(gBox); for (int i = 0; i < num; i++) rb.Add(new RadioButton()); rb[i].AutoSize = true; rb[i].Location = new Point(15, 20 + hsep * i); rb[i].Name = "radioButton" + Convert.ToString(i); rb[i].Text = "This is Radio Button " + Convert.ToString(i); gBox.Controls.Add(rb[i]); } btnBuildIt.Enabled = false; hsep will be used to set the separation in height between the radio buttons num is the number of Radio Buttons to be created
Form1 is resized to hold the new controls private void btnBuildIt_Click(object sender, EventArgs e) { int hsep = 40; int num = (int)numUDCount.Value; if (numUDCount.Value > 0) Form1.ActiveForm.Height = 115 + num * hsep; GroupBox gBox = new GroupBox(); gBox.Text = "Demo Group"; gBox.Size = new Size(200, 20 + num * hsep); gBox.Location = new Point(20, 20); this.Controls.Add(gBox); for (int i = 0; i < num; i++) rb.Add(new RadioButton()); rb[i].AutoSize = true; rb[i].Location = new Point(15, 20 + hsep * i); rb[i].Name = "radioButton" + Convert.ToString(i); rb[i].Text = "This is Radio Button " + Convert.ToString(i); gBox.Controls.Add(rb[i]); } btnBuildIt.Enabled = false; Form1 is resized to hold the new controls a GroupBox is created to hold the Radio Buttons gBox is resized to hold i Radio Buttons gBox is located on the main Form gBox is added the to this Form's set of Controls
this loop will create num Radio Buttons rb[i] private void btnBuildIt_Click(object sender, EventArgs e) { int hsep = 40; int num = (int)numUDCount.Value; if (numUDCount.Value > 0) Form1.ActiveForm.Height = 115 + num * hsep; GroupBox gBox = new GroupBox(); gBox.Text = "Demo Group"; gBox.Size = new Size(200, 20 + num * hsep); gBox.Location = new Point(20, 20); this.Controls.Add(gBox); for (int i = 0; i < num; i++) rb.Add(new RadioButton()); rb[i].AutoSize = true; rb[i].Location = new Point(15, 20 + hsep * i); rb[i].Name = "radioButton" + Convert.ToString(i); rb[i].Text = "This is Radio Button " + Convert.ToString(i); gBox.Controls.Add(rb[i]); } btnBuildIt.Enabled = false; this loop will create num Radio Buttons rb[i] Radio Buttons are separated by hsep in height Radio Button rb[i] is added to gBox.Controls