0

I have a series of textbox with a label name and a sequential number eg entry_1 entry_2 exit_1 exit_2

i would like to loop through each of these to check if they have values and retrieve value

something like this:

for (int i = 1; i <= 2; i++)

entry =  "entry_" + i.text
exit = "exit_" + i.text

Any assistance would be much appreciated

user4891693
  • 35
  • 1
  • 2
  • 5

1 Answers1

0
           List<string> Values = new List<string>();
            foreach (TextBox txt in this.Controls)
            {
                if (!string.IsNullOrWhiteSpace(txt.Text))
                {
                    Values.Add(txt.Text);
                }
            }

                or

            int countOfTextBoxes= //Your TextBox Count;
            TextBox txt;
            for (int i = 1; i <= countOfTextBoxes; i++)
            {
                txt = (TextBox)this.Controls["entry_" + i];
                if (!(string.IsNullOrWhiteSpace(txt.Text)))
                {
                    //Your Code For Retreiving Texts
                }
            }
yigitt
  • 778
  • 3
  • 9
  • 16
  • thanks ufuk, when i try the second option i get nullreferenceexception on txt. object reference not set to an instance of an object . what do i need to include to prevent this? – user4891693 Mar 13 '16 at 03:08
  • You can simply Do a null Check. For Second Option if txt Comes Null , that means you enter wrong number Of textBox Amount or TextBox Name does not Match entry_i Format. Add one more if Clause like if (this.Controls["entry"+i] != null) {All Code İnside For Loop Comes Here } – yigitt Mar 13 '16 at 10:39
  • for some reason the txt continue to returns null...... i have replaced the txt.Text with entry_1.Text and everything is working fine. The issue i something in txt, but i cannot find the issue – user4891693 Mar 13 '16 at 14:05