Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Controls Dynamically in C#

Similar presentations


Presentation on theme: "Creating Controls Dynamically in C#"— Presentation transcript:

1 Creating Controls Dynamically in C#

2 Dynamically Generated Controls
the user selects the number of radio buttons to generate and then presses the Build Itt button

3 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 = 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;

4 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 = 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

5 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 = 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

6 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 = 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


Download ppt "Creating Controls Dynamically in C#"

Similar presentations


Ads by Google