Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to index dynamically created controls and their values?

How to index dynamically created controls and their values?

Scheduled Pinned Locked Moved C#
questiondatabasegraphicstutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    turbosupramk3
    wrote on last edited by
    #1

    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;
    

    }

    L 1 Reply Last reply
    0
    • T turbosupramk3

      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;
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups