Search code examples
c#objecttextboxnames

Increase object names programmatically C#


I have a textbox for data entry and 10 textboxes for showing data. 10 viewer textboxes are visible=false by default. For example when I enter textbox count to be "3" , only 3 textboxes should be visible. (Then I can do whatever I want with those textboxes)

Example

Here's my code;

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());

SqlCommand pencere = new SqlCommand("select COUNT (distinct(grup)) as ürün from fiyat", conn);
SqlCommand pencereisimleri = new SqlCommand("select distinct(grup) as ürün from fiyat", conn);
conn.Open();
SqlDataReader dr = pencere.ExecuteReader();
SqlDataAdapter da = new SqlDataAdapter(pencereisimleri);
DataTable dt1 = new DataTable();
da.Fill(dt1);

List<String> stringArr = new List<String>();

for (int a = 0; a < dt1.Rows.Count; a++)
{
    TextBox[a].Visible = true;
    TextBox[a].Text = "Open Textbox";                    
}

Solution

  • Not sure, if you're going for this, but how about adding the text boxes to a collection, like an array or List?

    Example:

    private List<TextBox> boxes = new List<TextBox>();
    boxes.Add(textBox1);
    boxes.Add(textBox2);
    // etc...
    

    Then you could use a foreach (or for) loop, to go through your boxes:

    for (int a = 0; a < dt1.Rows.Count; a++)
    {
        boxes[a].Visible = true;
        boxes[a].Text = "Open Textbox";
    }