public void FBD(int I) { FolderBrowserDialog FBD = new FolderBrowserDialog(); DialogResult result = FBD.ShowDialog(); if (result == DialogResult.OK) { Formas.textBox[I].Text = FBD.SelectedPath; Formas.toolStripStatusLabel1.Text = "Настройка"; } } 

How correctly to address to i elements in functions? Gives an error message

"Install.Form1" does not contain a definition for "textBox". Could not find extension method "textBox" that takes the first argument of type "Install.Form1" (missing the using directive or link to the assembly?)

  • Do you have a Formas field or property in textBox ? If not, it is not clear what surprises you. - VladD

2 answers 2

Why reinvent the wheel?

Use this:

 foreach(var pb in this.Controls.OfType<Твой тип>()) { //do stuff } 

If Formas in your code is a Form heir, then you can refer to the i-th element like this:

 Formas.Controls.OfType<Твой тип>()[i] 
  • Is there an example? I can't do it - aaa
  • And what exactly does not work? On the form in some method just need to copy my code. - iluxa1810
  • Show an example on my code, I could not access the i element. - aaa
 private List<TextBox> GetTextBoxesFrom(Control parent) { List<TextBox> result = new List<TextBox>(); List<TextBox> indirectChildren = null; foreach (Control c in parent.Controls) { //check for the base case if (c is TextBox) result.Add((TextBox)c); else if (c.HasChildren) // it's a container { indirectChildren = GetTextBoxesFrom(c); result.AddRange(indirectChildren); } } return result; } var textBoxList = GetTextBoxesFrom(this); textBoxList[0].Text = "0";