copy one listbox from memory to a window

I’m creating different Listboxes and in an output window I want to display a specific one based on some (user) settings. Code would look like this

[code]function a
dim LBa as new listbox
// fill with data

function b
dim LBb as new listbox
// fill with data

then I have an existing designed window with a Listbox, and depending on some if-then-else routine I want to display a specific output, I tried like this:

Window2.Listbox1=LBa Window2.Show

the debugger states Window2.Listbox1 does not exist. But if I try the code:

Window2.Listbox1.ColumnCount=LBa.ColumnCount Window2.Listbox1.AddRow("test") Window2.Show

This works fine. So Window2.Listbox1 does exist. Now I know that I can build a whole for…next loop to do a Listbox.Addrow for every row in LBa/LBb and so on, but can’t I just set the Listbox in the Window2 equal to the Listbox I created in memory? Or is this not possible?


Window2.ListBox1.Cell(-1, -1) = LBa.Cell(-1, -1)

No, you cannot.

ok I found a little more sever issue with my Listboxes.
They are defined in a Module as a Property (just like my other global variables). I initialize them as follows

LBa = new Listbox

then I can fill data in with AddRow, but I noticed if I try to see what is in there (MsgBox of the LBa.LastIndex), it’s empty. This might be part of the problem?

@Wayne, your code solves the error, so the code compiles, just no output with the issue above… once I understand this issue I’ll check if you’re code does what I try to do.

This simply isn’t valid code. You cannot create a listbox (or any other control) out of thin air like this. Clone an existing control on the window (and keep it invisible) or use a ContainerControl.

@Tim, I remember doing this in RB, though… a very long time ago (probably late 90s). Anyway, the compiler doesn’t generate an error, so I didn’t see anything wrong with it. Now I am looking at the ContainerControl which I think should do the job, as I can just add LBa, LBb and so on to them and get the results from them into the main window (hopefully with Wayne’s trick), however, in the main window’s code I can’t seem to access the ContainerControl, something like:


or any related code raises the error that the item does not exist, highlighting LBa. Nor AddRow, and so on, so somehow it can’t get to the ControlContainer. I looked at Xojo’s online help but I’m kind of lost as to why I can’t access a listbox from a Window when I place that listbox in a ContainerControl?

Make sure you’re addressing an instance of the container, not the container definition itself.

cc = new MyContainerControl
cc.LBa.ColumnCount = 7

would it not be:

cc = new MyContainerControl1

either way, cc.LBa.ColumnCount does not exist…

Does the container contain a listbox named LBa? Is it Public or Private?

But an even better approach would be to pass the real listbox to function a and function b.

function a(lb as ListBox)
   lb.ColumnCount = 7
   // fill with data

function b(lb as ListBox)
   lb.ColumnCount = 7
   // fill with data

if something then

Tim, the listbox LBa in ContainerControl1 has Scope “Public”.

I see what you’re on to with above example. Let me explain in more detail what I am doing. I’m reading a rather large file with different sorts of data. I was thinking of loading them in a Listbox rather than array because the file doesn’t contain info upfront about the datasize, and “array.append” will only work on a one dimensional array, while “listbox.addrow” works irrespective of columncount.

After loading the data into memory, I would like the user to chose which data to view (LBa, LBb, and so on). If the file would always contain the same datatypes (let’s say only LBa, LBb and LBc), then of course I could hard code it into WindowA.ListboxA, WindowB.ListboxB, and WindowC.ListboxC. But I prefer to be flexible to any sort of data I can come across and have a generic window (the Window2 in above code), and have Window2.Listbox1 accept any data I loaded into memory (LBa, LBb etc).

Your code will work, if in function a (b) I would set the Window2.Listbox1 its size equal to LBa (LBb), and fill cell by cell. I worry that could be a slow exercise…that is why I was hoping to have a piece of code where I could just say, Window2.Listbox1=LBa
Now I like to try Wayne’s suggestion, but now run into the problem that LBa in my ContainerControl remains empty upon an AddRow…

following up to Wayne’s suggestion, the following code

[code] LBa.AddRow(“test”)

Window1.ListBox1.Cell(-1, -1) = LBa.Cell(-1, -1)

just gives an empty screen. So sadly that didn’t work. However, I also found an interesting bug. In the debugger, when you try to see the contents of LBa, it crashes the debugger, quite repeatable. I wonder if something isn’t working the way it should with these listboxes…

expanding on this code:

[code] dim i As Integer

dim LBa as new Listbox

for i=1 to 1000
LBa.AddRow("test "+str(i))
next i

for i=0 to LBa.LastIndex
next i[/code]

gives an empty listbox. Adding one line:


shows Listbox1 having just one row (“check”)


returns -1, so that whole for…next didn’t add a single row.

My conclusion is sadly that dynamic listboxes, although compiling correctly, are not working at all. I don’t see anything wrong with above code and neither does the compiler. Is this a bug? Can anyone try above code or explain me why it doesn’t work?

Yeah don’t try & use a listbox as temporary storage like this.
You’ll be immensely frustrated and will quite possibly experience “bugs”.
UI controls (like list boxes, pushbuttons etc) are not great in memory “data containers” that can be passed from one window to another nor can they be instantiated as you’ve done - they don’t get set up correctly.


  1. create a class for each “type” of data that can be read in (whatever an LBa would show should be one class and what ever LBb would display is another)
  2. read the data into an array of these classes
  3. reconfigure the single instance of the listbox to have however many columns etc are required to show the array of data

Even if you say “but I don’t know what data might be in there” I’d still create a class that can have, for one row, a key & value for an arbitrary number of columns of data. And then you can still display that in the single listbox on the window.

@Norman, I actually think this would work just fine, let me try it out. I do have a question about #2, what do you mean with an array of classes, was that a figure of speech, or did you mean it litterally to create an array of these classes?

thanks for the suggestions.

literall create an array of classes

say you create a class called “MyClass”
you can create an array of them with

dim arr(-1) as MyClass
arr. append new MyClass

And then each element of the array is an instance of your custom class with all its properties etc

FYI - points 1, 2, 3 are the components of ONE suggestion :stuck_out_tongue: