How to index dynamically created controls and their values?
-
I have dynamically created controls that I want to use to create and write an output file with. How can I organize the controls values based on their name?
private TextBox txtBox1 = new TextBox();
private ComboBox cmbx1 = new ComboBox();
private TextBox txtBox2 = new TextBox();
private TextBox txtBox3 = new TextBox();
private Button btn1 = new Button();
int configLineIndex = 1;
private void btnCuRepairConfigAddLine_Click(object sender, EventArgs e)
{
configLineIndex++;
tbxHorizontalIndex = 21;txtBox1 = new TextBox(); txtBox1.Name = "line" + configLineIndex + "a"; int tbxWidth = 285; this.txtBox1.Location = new System.Drawing.Point(tbxHorizontalIndex, tbxVerticalIndex + 30); this.txtBox1.Size = new System.Drawing.Size(tbxWidth, 20); this.Controls.Add(txtBox1); cmbx1 = new ComboBox(); cmbx1.Name = "line" + configLineIndex + "b"; this.cmbx1.Location = new Point(tbxHorizontalIndex + 299, tbxVerticalIndex + 30); this.cmbx1.Size = new Size(121, 21); this.cmbx1.Items.Add("addAfter"); this.cmbx1.Items.Add("addBefore"); this.cmbx1.Items.Add("Replace"); this.Controls.Add(cmbx1); txtBox2 = new TextBox(); txtBox2.Name = "line" + configLineIndex + "c"; tbxWidth = 320; this.txtBox2.Location = new System.Drawing.Point(tbxHorizontalIndex + 435, tbxVerticalIndex + 30); this.txtBox2.Size = new System.Drawing.Size(tbxWidth, 20); this.Controls.Add(txtBox2); txtBox3 = new TextBox(); txtBox3.Name = "line" + configLineIndex + "d"; tbxWidth = 320; this.txtBox3.Location = new System.Drawing.Point(tbxHorizontalIndex + 770, tbxVerticalIndex + 30); this.txtBox3.Size = new System.Drawing.Size(tbxWidth, 20); this.Controls.Add(txtBox3); btn1 = new Button(); btn1.Name = "line" + configLineIndex + "e"; this.btn1.Location = new Point(tbxHorizontalIndex + 1110, tbxVerticalIndex + 30); this.btn1.Size = new Size(20, 20); this.btn1.Text = "X"; this.Controls.Add(btn1); //tbxHorizontalIndex = tbxHorizontalIndex + tbxWidth + 10; tbxVerticalIndex = tbxVerticalIndex + 30;
}
-
I have dynamically created controls that I want to use to create and write an output file with. How can I organize the controls values based on their name?
private TextBox txtBox1 = new TextBox();
private ComboBox cmbx1 = new ComboBox();
private TextBox txtBox2 = new TextBox();
private TextBox txtBox3 = new TextBox();
private Button btn1 = new Button();
int configLineIndex = 1;
private void btnCuRepairConfigAddLine_Click(object sender, EventArgs e)
{
configLineIndex++;
tbxHorizontalIndex = 21;txtBox1 = new TextBox(); txtBox1.Name = "line" + configLineIndex + "a"; int tbxWidth = 285; this.txtBox1.Location = new System.Drawing.Point(tbxHorizontalIndex, tbxVerticalIndex + 30); this.txtBox1.Size = new System.Drawing.Size(tbxWidth, 20); this.Controls.Add(txtBox1); cmbx1 = new ComboBox(); cmbx1.Name = "line" + configLineIndex + "b"; this.cmbx1.Location = new Point(tbxHorizontalIndex + 299, tbxVerticalIndex + 30); this.cmbx1.Size = new Size(121, 21); this.cmbx1.Items.Add("addAfter"); this.cmbx1.Items.Add("addBefore"); this.cmbx1.Items.Add("Replace"); this.Controls.Add(cmbx1); txtBox2 = new TextBox(); txtBox2.Name = "line" + configLineIndex + "c"; tbxWidth = 320; this.txtBox2.Location = new System.Drawing.Point(tbxHorizontalIndex + 435, tbxVerticalIndex + 30); this.txtBox2.Size = new System.Drawing.Size(tbxWidth, 20); this.Controls.Add(txtBox2); txtBox3 = new TextBox(); txtBox3.Name = "line" + configLineIndex + "d"; tbxWidth = 320; this.txtBox3.Location = new System.Drawing.Point(tbxHorizontalIndex + 770, tbxVerticalIndex + 30); this.txtBox3.Size = new System.Drawing.Size(tbxWidth, 20); this.Controls.Add(txtBox3); btn1 = new Button(); btn1.Name = "line" + configLineIndex + "e"; this.btn1.Location = new Point(tbxHorizontalIndex + 1110, tbxVerticalIndex + 30); this.btn1.Size = new Size(20, 20); this.btn1.Text = "X"; this.Controls.Add(btn1); //tbxHorizontalIndex = tbxHorizontalIndex + tbxWidth + 10; tbxVerticalIndex = tbxVerticalIndex + 30;
}
you could start by giving your controls meaningful names so anyone can better guess what they are used for. You can list all the controls on your form by something like:
foreach (Control cc in Controls)
{
// access the control's properties
// check its type etc.
}See Control Class (System.Windows.Forms)[^] for Properties, Methods etc.