AlucardF0X
AlucardF0X

Reputation: 31

Control.ControlCollection issue

I'm working with Microsoft Visual Studio Express 2010 Developing a Windows Forms application (C#)

I'm bamboozled by the following:

I create a class for organizing my data

namespace Project_Management_Wizard {
public class dataStorage {
    public System.Windows.Forms.Control wizard_tabs;

    }
}

I then put data in it

data.wizard_tabs = new Wizard_Tabs().Controls[0];

Wizard_Tabs is a Form that has one control on it - a System.Windows.Forms.Tab.Control.

So basically, now, data.wizard_tabs is a Tab.Control, which has several tabs with contents in them.

I then fetch those contents and put them on another form.

This helps me organize my form contents.

I do this by

   for(int I=0; I < data.wizard_tabs.Controls[Page].Controls.Count; I += 1) {
   this.Controls.Add(data.wizard_tabs.Controls[0].Controls[I]);
   }

Basically, this goes through a page's contents and adds every element that is in the page to the form I'm working in.

Basically, this should make the form I'm working be the tab's contents.

However, I ran into a bizzare problem.

Controls.Count as above returns "5", and I have exactly 5 elements on the tab, so that's correct.

I have two labels and three group-boxes.

However, when the loop runs, it adds elements 0, 2, 4; and skips 1, 3. I checked in detail, Controls[1], Controls[3] have nothing in them. It's basically skipping every 2nd element.

To demonstrate this, I added a bunch of labels down at the bottom. Here is the wizard tabs thingy where I store my form contents:

![enter image description here][1]

And it turns out to be this after the code above is executed:

![enter image description here][2]

As you can clearly see, every 2nd element is just "skipped".

Any tips on how I can make C# give me the contents from the tab, without skipping every 2nd element for some reason?

I'm storing the TabControl's Contents data as a Control.ControlCollection.

I'm currently trying to figure out if it's a problem that occurs during saving the data, or generating the form, etc. I'll edit here if I figure this out.


Tests:

this.Controls.Add(data.wizard_tabs);

Successfully adds the complete tab control and everything within it to the form.

this.Controls.Add(data.wizard_tabs.Controls[0].Controls[0]);

Successfully adds element #0 from the tab to the form.

        this.Controls.Add(data.wizard_tabs.Controls[0].Controls[0]);
        this.Controls.Add(data.wizard_tabs.Controls[0].Controls[1]);
        this.Controls.Add(data.wizard_tabs.Controls[0].Controls[2]);
        this.Controls.Add(data.wizard_tabs.Controls[0].Controls[3]);
        this.Controls.Add(data.wizard_tabs.Controls[0].Controls[4]);

Seems to give me every 2nd element.

Somehow, after accessing a Tab's contents, every 2nd control is removed.

Even though controls gives me a count of 9, which are all the elements within the tab, trying to access them gives me an error out of index.

If there's 9 elements, you'd expect [0][3]...[8] to be there, but [5]..[8] aren't there.


    for(int I=data.wizard_tabs.Controls[Page].Controls.Count-1; I >= 0; I -= 1) {
        this.Controls.Add(data.wizard_tabs.Controls[0].Controls[I]);
        }

Seems to have fixed the issue, and it seems this.ControlsAdd() seems to mess with the itself, somehow.

Thanks plenty!

But now I'm looking for a relatively simpler way of doing this.

I have the ControlCollection I want to display as data.wizard_tabs.Controls[0].Controls

So is there an explicit way to "this.Controls = data.wizard_tabs.Controls[0].Controls"?


            for(int I=0; I < 9; I += 1) {
            this.Controls.Add(data.wizard_tabs.Controls[0].Controls[0]);
            }

This also seems to do the job.

Apparently, Controls.Add(control) adds the control to the current element AND REMOVES the control from the source it's reading it from, collapsing the Collection where it took the element from.

I have no idea why it's completely undocumented that it does that, nor why would it need to do this.

Upvotes: 2

Views: 1442

Answers (1)

AlucardF0X
AlucardF0X

Reputation: 31

Apparently, Controls.Add(control) adds the control to the current element AND REMOVES the control from the source it's reading it from, collapsing the Collection where it took the element from.

I have no idea why it's completely undocumented that it does that, nor why would it need to do this.

Code to solve the problem and demonstrate it clearly is:

    for(int I=0; I < 9; I += 1) {
    this.Controls.Add(data.wizard_tabs.Controls[0].Controls[0]);
    }

Upvotes: 1

Related Questions