Beware of Dynamically Loaded Containers

This one has me scratching my head. I even had Robert review to make sure I wasn’t being stupid (insert snarky comment here). I have the following code to load a PopupMenu:

[code] while rs.eof = false
dim sText as string = rs.idxField(1).StringValue
dim v as variant = rs.idxField(2).value

pmList.addrow sText
pmList.RowTag(pmList.ListCount-1) = v

rs.MoveNext

wend[/code]

I can see v in the debugger, but if I try to access it in the RowTag it always comes back nil. Weird. I add a dictionary like this:

[code]while rs.eof = false
dim sText as string = rs.idxField(1).StringValue
dim v as variant = rs.idxField(2).value

pmList.addrow sText
pmList.RowTag(pmList.ListCount-1) = v

dictList.value(sText) = v

rs.MoveNext

wend[/code]

And then test the values like this;

dim v as variant = dictList.value(pmList.text) dim v2 as variant = pmList.RowTag(pmList.ListIndex)

v has the correct value and v2 is always nil. Only one spot we’re being loaded so there’s no interference from anything else. Very bizarre. Just curious if anyone has run across anything like this before.

Alrighty then. One bit of information I failed to mention is that this control is on a dynamically loaded ContainerControl. The Load happens before the EmbedWithin happens.

When I switch the order so it does the EmbedWithin first and then the load it works as expected.

I’ll see if I can replicate in a small project and submit.

Sorry for the waste of bandwidth.

You may better use pmList.lastindex instead of pmList.ListCount-1.
You don’t always add to the end.

Ah, no problem.

The thing with EmbedWithin is that Xojo delayes creating of underlaying objects until the EmbedWithin.
So while you have a listbox there, it’s not yet fully constructed.

It’s a PopupMenu.

Regardless, the menu items are there and selectable. It just has no row tag.

feedback://showreport?report_id=42620

This is not a bug. A control is ready when the Open event is raised by the framework. And Open will only be called when EmbedWithin is processed. The listbox might be there, but not in a state to be “used” (only the constructors have been called). You see this when logging with CurrentMethodName:

cc.load("Load Before Embed") cc.EmbedWithin self, me.left, me.top + me.Height + 10, cc.width, cc.Height –> PopupMenu1.Constructor ContainerControl1.Constructor ContainerControl1.Load // Controls are not ready here ContainerControl1.PopupMenu1.Open ContainerControl1.Open

cc.EmbedWithin self, me.left, me.top + me.Height + 10, cc.width, cc.Height cc.load("Load After Embed") –> PopupMenu1.Constructor ContainerControl1.Constructor ContainerControl1.PopupMenu1.Open ContainerControl1.Open ContainerControl1.Load // This will work

Hm…well, it’s semantics, IMO. The Row TEXT is being populated but not the RowTag.

Granted, it probably should have never worked in the first place, but the seven dynamic containers that use different controls all passed testing. It’s only the PopupMenu that does this.

After the Load method has been executed the popup menu has no entries in the debugger. The control is just not ready before the Open event – it might work in some cases, but this is pure luck. The docs state for the Open event: “The window is about to open. Use this event to initialize a control.”

[quote=248075:@Bob Keeney]Alrighty then. One bit of information I failed to mention is that this control is on a dynamically loaded ContainerControl. The Load happens before the EmbedWithin happens.

When I switch the order so it does the EmbedWithin first and then the load it works as expected.

I’ll see if I can replicate in a small project and submit.

Sorry for the waste of bandwidth.[/quote]
Dont load controls BEFORE you embed the container they live on
This isn’t new

[quote=248146:@Norman Palardy]Dont load controls BEFORE you embed the container they live on
This isn’t new[/quote]
Well, sure. I’m surprised it worked, period. Like I said, the other dynamic controls worked no issue using the incorrect method. And with the exception of the RowTag property so did this popup menu.

Hopefully, someone learns from my stupidity and runs across this thread and figures it out sooner than I did.

I too have run into funky issues with execution order; however not this situation. Now I know; so it’s not wasted bandwidth and I do not consider you stupid for sharing.

Although I know how you feel…

And really, this should be titled Beware of Dynamically Loaded Containers (done). You need to be careful of when you start working with controls.