Dynamic Container Controls

I am adding a container control in at runtime on my page like so:

Dim top as Integer = 762 Dim left as Integer=20 test = New TestContainer test.EmbedWithin(Self, left, top, test.Width, test.Height)

I loop through and add whatever number I need. They show up no problem and are there and I can enter whatever I want in them.

In order to access them I created a property in the page that subclasses from my TestContainer and I add what I created shown above like so:

mQuestions.Append test

However, elsewhere in code I need to access the data that is in those fields so I do that like this:

Dim tp As New TestPage Dim mydata As String mydata = tp.mQuestions(x).DataEntry.Text

But whenever I try to access anything from those fields, there is no data at all, even when I debug it and step through there is nothing in those fields.

How am I suppose to grab the data from these fields?


You’re not addressing the actual page that contains them.

Well I tried that originally and went the route shown in my question which still didnt work. I thought perhaps I needed a new instance of the page in order to get what I needed.

I really have no idea even if I did it in code referencing TestPage the only thing that shows up is testcontainer and no real way to access the textfield that I have it in via the page.

Where does the original page come from? Is it the default page for the app? Do you create it in code? If you create it in code, you need to keep a reference to it. Where is the code that tries to get at the data? In another page? If so, how did you get there? Did the original page show that page? Or what?


I show the TestPage via the code below from another page button action:

Dim theTestPage As New TestPage theTestPage.Show

In a method that is called via the open event of that page is where I add the TestContainer to the page. In another container is where I am trying to access the TestPage.mQuestions().DataEntry.Text info where mQuestions is a subclass of TestContainer so I can keep track of them like an array since I cannot use control sets in a web app.

Boy, do I remember wrestling with that one to begin with … wore the word “Google” out on my keyboard … but got it with the help of various things I found in the forum here. So, let’s see if I can “pay it forward”.

I’m doing this all from memory here, but I think it should be correct

[code]// create a module and add the following arrays as global properties of the module …
// oContainers() as type ContainerControl and oContainerIndex() as String data type
// each time you append another container control to the array oContainers, you add a reference to it in the array oContainerIndex.
// That way, the index of each is consistent, e.g., oContainers(0) contains the 1st appended container control (cntElectrical in the case below)
// while oContainerIndex(0) contains the reference to it (“ec” in the case below)

Dim xCoord As Integer = whatever x coordinate you want for the container once embedded
Dim yCoord As Integer = whatever y coordinate you want for the container once embedded

Dim ec As New cntElectrical
ec.EmbedWithin(wndMainWindow,xCoord,yCoord) // place container in main window

oContainers.Append ec // place container in array for future control
oContainerIndex.Append(“ec”) // THIS IS THE KEY YOU’RE MISSING … add ability to ID each container in the array oContainers later

// Now when you wish to use any contents, since you know what “references” you assigned to the container controls, you just do the following:
// Loop thru oContainerIndex to find the reference you want … use the same index to access its associated container control in oContainers

for idx As Integer = 0 to Ubound(oContainerIndex)
if oContainerIndex(idx) = “ec” then
MsgBox (cntElectrical(oContainers(idx)).txfXYZ.Text) // this is how you’d get the text from a text field control named txfXYZ in the container control “ec”
oContainers(idx).Close // this is an example of how you’d work directly with the associated container control itself
exit /// since you’ve found the container you’re looking for
end if

// Obviously, once you have the index of the container control you want, you can continue to do other operations
// on it without looping thru the index array again[/code]

Hope that helps!
… Don

Well I am pretty much doing it the same way. I am just adding a property to the page called mQuestions that is a subclass of TestContainer. I then in code add the test container to that subclass when I add it to the page like so:

test = New TestContainer test.EmbedWithin(Self, left, top, test.Width, test.Height) mQuestions.Append test

So from anywhere else in the app I should be able to reference the page and that property like so:


I would think that is correct.


This creates a local variable that you lose control of right away. You need to save this in a property that doesn’t go out of scope, so it is available in the other method. But don’t use a module, that would be global across sessions. Put the property the original page (the one with the button that creates the TestPage) or in the session.

So in the page that calls the TestPage put a Property called MyPage and reference the TestPage?

I realize I can just do:


and that shows the page as well. Not sure what made me do it the other way.

Yes. Either

 Dim theTestPage As New TestPage
 MyPage = theTestPage

Or, more simply

MyPage = New TestPage

Then you can refer to

mydata = MyPage.mQuestions(x).DataEntry.Text