Hi,
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[30];
TextBox[] newtxts = new TextBox[30];

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.

Anybody please?

Do you remove the labels from the control collection before you dispose of them? The behavior you are experiencing leads me to believe that the controls are still referenced somewhere.

Thanks,
I'm not sure how to remove them, as they are only added using add controls.
I thought disposing would remove them.

The Forms ControlCollection has a Remove method. If you need the controls later just call the Add method again.
Perhaps you can do something with the Visible property of the Label and TextBox controls?

Thanks,
I'm not sure how to remove them, as they are only added using add controls.
I thought disposing would remove them.

This is a common misconception. Disposing of an object (calling the Dispose method) just gets rid of the unmanaged resources used by the object. This also places the object in an unusable state so any calls to the methods of the object should result in an exception.

Objects don't know what references them, so there is no way for them to force other objects to stop referencing them. You have to do that. This includes standard references (like the control collection), events, timers, etc.

Look at this code, it creates and erases (if exist) all labels:

Label[] label;
        int counter = 1;
        private void button1_Click(object sender, EventArgs e)
        {
            //checing if labels already arecreatred:
            if (label != null)
                ClearingLabels();
            
            //next: creating new labels:
            int x = 20;
            int y = 20;
            label = new Label[5];
            for (int i = 0; i < label.Length; i++)
            {
                label[i] = new Label();
                label[i].Location = new Point(x, y);
                label[i].Text = "My label number " + ((i + 1) * counter).ToString();
                this.Controls.Add(label[i]);

                //setting new location for the next label:
                y = y + 25;
            }

            //to check that new labels are created:
            counter++;
        }

        private void ClearingLabels()
        {
            for (int i = 0; i < label.Length; i++)
            {
                label[i].Text = "";
                this.Controls.Remove(label[i]);
            }
            label = null;
        }

Thanks very much to all who responded.
Your answers were of great help to me. I never realised I could remove controls, as well as add. Of course looking at it now it seems obvious, but it wasn't obvious to me before.

Thanks again

hehe, this is not your last time to be surprised regarding programmning, you can be sure about that. There is still much to come... gl
Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.