I have a problem with filling a form dynamically.
I have a form to which I add labels and textboxes.
They are dimensioned globally as
Label newlbls = new Label;
TextBox newtxts = new TextBox;
The first time I add controls there are four labels required. Everything works fine, after I add the controls to the form. But then I dispose of all the controls and run it a second time with only three labels, the problem is the form continues to see the label array as having four labels, and as I only create three new labels. It throws an object disposed exception.
The problem seems to be in the first run there are 4 labels in the second run there are only 3, so the fourth one throws an exception.
I have tried setting array to null. Fails because then can't create new label.
I've tried disposing of label also doesn't work.
I've tried setting each element to null, also doesn't work.
I realize if I dimensioned the label array in a method, it might fix it, but then I can't access label outside the method.