Search code examples
c#textboxcontrolsfetchvisual-studio-2019

C# fetch textbox with variable in name


i have 8 textboxes in my code that are visible =false. Now i want to set them visible after a button click event.

The textbox names are:

tb1 tb2 tb3 ...

I want fetch the textbox with tb+i (i=1) and make them visible. After the first textbox is visible, there is an i++, so every click just show one textbox more.

I already tried that, but it didnt worked:

 TextBox textbox = (TextBox)Controls.Find(string.Format("tb"+i), false).FirstOrDefault();

        textbox.Visible = true; */
        i++;

Thank you.


Solution

  • Now i have the solution:

     if (i < 9) { var textbox = Controls.Find("tb" + i, true).FirstOrDefault(); if (textbox != null) { textbox.Visible = true; } i++; }