I have to write a program (C#, WPF), where data is retrieved from ~30 TextBoxes. I’d like to cycle the textboxes through. I tried to create an array of textboxes, but it didn’t work very well because in every method I had to repeatedly reinitialize this array.
TextBox[] subjects = { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9, textBox10 };
TextBox[] credits = { textBox11, textBox12, textBox13, textBox14, textBox15, textBox16, textBox17, textBox18, textBox19, textBox20 };
TextBox[] marks = { textBox21, textBox22, textBox23, textBox24, textBox25, textBox26, textBox27, textBox28, textBox29, textBox30 };
Subject.SubjectName = subjects[selection].Text;
Subject.AmountOfCredits= Convert.ToInt32(credits[selection].Text);
Subject.Mark = Convert.ToInt32(marks[selection].Text);
Main question is, if there is any other way to cycle through all those controls without creating arrays of textboxes?
Thanks in advance.
Have you considered using a DataGrid control? You could have three columns (Subjects, Credits and Marks) and easily get to the selected record via the SelectedItem property?
The other option is to use an ItemsControl. You could style the ItemTemplate to have three textboxes, which you databind to the properties of Subject directly. The ItemsControl’s ItemsSource would then be bound to an observable collection of Subjects. For more information on how to do this, go to Microsoft’s help on Data Templating Overview.