PDA

View Full Version : Difficulty filling Listbox with Array



JSelf
2008-08-12, 02:03 PM
I am having an interesting problem filling a list box with the contents of an array. With the code below it only puts the last item in the array in the listbox. If I had for example 5 items in the list the first 4 would be blank and the 5th would be displayed as expected. Does anyone have any ideas as to what I am doing wrong?



DWGArrayCount = 1
For Each objFILE In objFLDR.Files
If InStr(1, objFILE.Name, ".dwg", vbTextCompare) > 0 Then
ReDim DWGArray(1 To DWGArrayCount)
CurrentDWG = objFILE.Name
DWGArrayString = Replace(CurrentDWG, ".dwg", "")
DWGArray(DWGArrayCount) = DWGArrayString
DWGArrayCount = DWGArrayCount + 1
End If
Next

For Each DWGName In DWGArray
TrayForm.ListBoxBlocks.AddItem DWGName
Next
TrayForm.ListBoxBlocks.ListIndex = 0


Thanks in advance,
Jason Self

glen.r80
2008-08-12, 02:45 PM
u could use a for-next loop from 0 to UPPERBOUNDS of the array (ubound function)
example>>>>>

examplelist = Array("one", "two", "three", "four", "five")
For i = 0 To UBound(examplelist)
ListBox1.AddItem examplelist(i)
Next i

Ed Jobe
2008-08-12, 03:54 PM
In addition to what Glen said...

As to what you are doing wrong, the syntax you are using to iterate the array. First, you can't iterate an array, only collections. I'm surprized you didn't get an error. You may have, but it could be masked by OnError Resume Next.

Second, the syntax is only used for iterating collections of objects, not raw data. DWGName is not a class name is it?

You can use the Locals window in breakmode to examine the contents of your array to make sure its getting populated on each iteration of the For loop.

JSelf
2008-08-12, 07:48 PM
Thanks for the replies. I didn't give the whole code, it was an object, so the syntax worked.

As for the solution, It was because when I did the ReDim I did not ReDim Preserve so I lost the data in the array