ContainerControls and IndexOf

Been using Xojo since RealBasic 5.5 and I have been able to find solutions to my issues on the Forums or just hammering away at it until I fix them, but this one has me stumped.

I have one container control on a window, and I’m embedding two more container controls via code EmbedWithin, which works as expected pictured below.

I am referencing these two container controls in an Array Property:

PanelList(-1) As ContainerControl

What I want to do is check if the panel is already displayed and prevent the user from displaying it again, here is a simplified version of my code below.

Sub AddPanel(w As ContainerControl)
		  Dim panelTop As Integer
		  Dim panelHeight As Integer
		  Dim result As Integer = -1
		  Dim originalHeight As Integer = w.Height
		  
		  result = PanelList.IndexOf(w)

		  if result > -1 then
		    return
		  end if

		  w.EmbedWithin(TimerWindow.AccessoryPanel, 0, panelTop, TimerWindow.AccessoryPanel.width, panelHeight)
		  w.LockRight = true
		  w.LockBottom = true
		  w.EraseBackground = false
		  
		  PanelList.Append(w)
		  
		End Sub

But the result is always -1. That is, PanelList.IndexOf(w) always results -1. Yet in the debugger, the property “PanelList” is holding a reference to the two ContainerControls. Am I doing this right? This is based on the example I found in the Language Reference, only they are not using an array.

If I click the X to dismiss the “FTD” and “Notifications” ContainerControl, ContainerControl.close works, the control is removed and I remove the reference from my PanelList array.

I’m missing something here, but what? Sometimes I can’t see the forest through the trees.
By the way, this is an Autocross Timing and Scoring application that I have been working on for a long time! :slight_smile:

I hope this makes sense, 'cause I’m stuck…

Thank you, Doug.

I’m pretty sure IndexOf is reserved for base data types : integer, strings, double
not sure if you can use it for objects.

Edit : mmm: no it’s not I have examples with objects in arrays and they use indexof .

You say

result = PanelList.IndexOf(w)

But where do you actually add w to the list?

Below.

From http://documentation.xojo.com/index.php/Indexof it is pretty obvious IndexOf is string oriented.

Instead of using IndexOf, go through each element, and verify its name with Introspection, to see if it is already there.

IndexOf of works with objects.

I tested a stripped-down version of your code, and container controls existing in PanelList are found with IndexOf.

I’m guessing the code that calls your routine is creating a NEW container instance and that is why it fails.

w = New ContainerControl1
PanelList.Append(w)
result = PanelList.IndexOf(w)  // > -1 = found using the exact same object

w = New ContainerControl1
result = PanelList.IndexOf(w)   // -1= not found a different object of the same type

Walk through the array and use ISA to find a control of the same type.

[quote=286356:@Tim Hare]I’m guessing the code that calls your routine is creating a NEW container instance and that is why it fails.

w = New ContainerControl1
PanelList.Append(w)
result = PanelList.IndexOf(w)  // > -1 = found using the exact same object

w = New ContainerControl1
result = PanelList.IndexOf(w)   // -1= not found a different object of the same type

Walk through the array and use ISA to find a control of the same type.[/quote]

Yes, the code that calls this routine is creating a NEW container instance from a custom class. That’s it!
[quote=286341:@Markus Winter]You say

result = PanelList.IndexOf(w)

But where do you actually add w to the list?

Below.[/quote]

PanelList.Append(w)

At the very bottom of the routine.[quote=286338:@Jean-Yves Pochez]I’m pretty sure IndexOf is reserved for base data types : integer, strings, double
not sure if you can use it for objects.

Edit : mmm: no it’s not I have examples with objects in arrays and they use indexof .[/quote]

[quote=286343:@Michel Bujardet]From http://documentation.xojo.com/index.php/Indexof it is pretty obvious IndexOf is string oriented.

Instead of using IndexOf, go through each element, and verify its name with Introspection, to see if it is already there.[/quote]

“Any valid data type” in the Syntax table and "IndexOf is not case-sensitive, but it is encoding-sensitive. " does imply that. But running the test posted by Tim does work.

OK, time to re-think this. Thank you all for your replies. I appreciate it!

Doug

Looping through the ContainerControl Array using the Title property is a workable solution:

for i = 0 to panelCount if PanelList(i).Title = w.Title then return end if next

Granted that a Container Control doesn’t display a title, but the property is still accessible and by using a unique name, it works quite well!

Thanks again.