0

i have about twenty text box in visual C# which their name are text box1,textbox2 and.... .

User inserts number in each text box and i want to read these numbers and save them in array like a[].

I tried this code but it took long time to do for all array component

a[0]=Convert.ToInt32(textBox1.Text);

Is there any way which i use "for" loop that computer read automatically read text box and save the number in a[] array in order?

thank you

Yav.Amsh
  • 3
  • 1
  • something like this might help: https://stackoverflow.com/questions/5883282/binding-property-to-control-in-winforms where you bind the textbox to a backing property instead (ie, an array of strings). – AJ X. Oct 05 '17 at 22:55
  • Possible duplicate of [Binding property to control in Winforms](https://stackoverflow.com/questions/5883282/binding-property-to-control-in-winforms) – Akhil Mathew Oct 06 '17 at 04:30

1 Answers1

1

Yes, use the Controls collection. Here is a simple example:

for (int i=0; i<= a.GetUpperBound(0); i++)
{
    var name = string.Format("textBox{0}", i+1);
    var textbox = this.Controls[name] as TextBox;
    a[i] = textbox.Text;
}
John Wu
  • 50,556
  • 8
  • 44
  • 80