I have been tasked in re-factoring some code on a web forms project and there is one section of code that I think could be combined into a single method and then passed a int and would modified the controls based on that. Here is the code:
txtline1_amnestyCheck.Text = "Y";
txtline1_range.BackColor = System.Drawing.Color.Azure;
txtline1_amnt.BackColor = System.Drawing.Color.Azure;
txtline1_minreqmet.BackColor = System.Drawing.Color.Azure;
txtline1_elizperdt.BackColor = System.Drawing.Color.Azure;
txtline1_amnestyCheck.BackColor = System.Drawing.Color.Azure;
txtline1_afterNSFamnt.BackColor = System.Drawing.Color.Azure;
txtline1_NSFamnt.BackColor = System.Drawing.Color.Azure;
There are twelve different fields sections of this ranging from txtline1 through txtline12. My hopes is to make this a single method that looks something like this:
private void ChangeTextlines(int i)
{
txtlinei_amnestyCheck.Text = "Y";
txtlinei_range.BackColor = System.Drawing.Color.Azure;
txtlinei_amnt.BackColor = System.Drawing.Color.Azure;
txtlinei_minreqmet.BackColor = System.Drawing.Color.Azure;
txtlinei_elizperdt.BackColor = System.Drawing.Color.Azure;
txtlinei_amnestyCheck.BackColor = System.Drawing.Color.Azure;
txtlinei_afterNSFamnt.BackColor = System.Drawing.Color.Azure;
txtlinei_NSFamnt.BackColor = System.Drawing.Color.Azure;
}
Where i would be the line number that is changing. Now is there a way for me to be able to do this or am I stuck with the 96 lines of code of having a group for each txtline?
If you know the parent control where they all sit then you could use it’s FindControl method to access each one.