2d array of variable length to Listbox

2d array of variable length to Listbox

Anonymous
Not applicable
453 Views
8 Replies
Message 1 of 9

2d array of variable length to Listbox

Anonymous
Not applicable
Hi,
I have an ascii file that I wish to bring into a list of 7 columns.
I tried creating the array, but as you would all know, it transposed the
array when I assigned the 2d array to the list, and I can't have the
variable array length as the first dimension otherwise the redim bombs out.

I tried creating a type record array, but the list doesn't like that object.

The array needs to redim'd with each line read of the file, which is of
unknown length.

How do you guys get an ascii delimited file into the listbox?

Thanks,

Jon Rasmussen
0 Likes
454 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable
Here's something I did a while back that works. Perhaps it will help in your situation.


Private Sub UpdateLayerList()
Dim oAcadLayer As AcadLayer
Dim oCurrentLayer As AcadLayer

Dim intColumn As Integer 'column index
Dim intRow As Integer 'row index
Dim intActiveLayerIndex As Integer

Set oCurrentLayer = ThisDrawing.ActiveLayer

'reset the list
lstLayerName.Clear

'redimension array to hold states of all layers
ReDim mstrLayerNameArray(ThisDrawing.Layers.Count - 1, 6)

'put all the layer names in list
For Each oAcadLayer In AcadLayers
mstrLayerNameArray(intRow, intColumn) = oAcadLayer.Name

intColumn = intColumn + 1

'determine freeze-thaw state
Select Case oAcadLayer.Freeze
Case "True"
mstrLayerNameArray(intRow, intColumn) = "Frozen"

Case Else
mstrLayerNameArray(intRow, intColumn) = "Thawed"
End Select

intColumn = intColumn + 1

'determine on-off state
Select Case oAcadLayer.LayerOn
Case "True"
mstrLayerNameArray(intRow, intColumn) = "On"

Case Else
mstrLayerNameArray(intRow, intColumn) = "Off"
End Select

intColumn = intColumn + 1

'determine unlocked - locked state
Select Case oAcadLayer.Lock
Case "True"
mstrLayerNameArray(intRow, intColumn) = "Locked"

Case Else
mstrLayerNameArray(intRow, intColumn) = "Unlocked"
End Select

intColumn = intColumn + 1

If oAcadLayer.Name = oCurrentLayer.Name Then
mstrLayerNameArray(intRow, intColumn) = "Active"
intActiveLayerIndex = intRow
End If

intColumn = intColumn + 1

mstrLayerNameArray(intRow, intColumn) = ConvertColorToString(oAcadLayer.Color)

intColumn = 0
intRow = intRow + 1
Next oAcadLayer

lstLayerName.List() = mstrLayerNameArray
lstLayerName.Selected(intActiveLayerIndex) = True
End Sub

Public Function ConvertColorToString(ByVal intColor As Integer) As String
Select Case intColor
Case 1
ConvertColorToString = "Red"

Case 2
ConvertColorToString = "Yellow"

Case 3
ConvertColorToString = "Green"

Case 4
ConvertColorToString = "Cyan"

Case 5
ConvertColorToString = "Blue"

Case 6
ConvertColorToString = "Magenta"

Case 7
ConvertColorToString = "White"

Case Else
ConvertColorToString = CStr(intColor)
End Select
End Function
0 Likes
Message 3 of 9

Anonymous
Not applicable
Thank you.
However, I need to be able to redim the number of rows as new information
comes available as I don't know the number of lines in the file.
I have considered passing the info directly to the listbox, using additem
and (while column .list()) but this seems cumbersome.
Is there any way to redim the row number dynamically?
Can I assign a 1 dimensional array to the next row of the listbox? I get an
error.
Or is there some way to pass a Type record (all strings) to the listbox?

Thanks

Jon Rasmussen
0 Likes
Message 4 of 9

Anonymous
Not applicable
I've settled on using a temporary single dim array for the current line of
data,
using additem to create a new list object, then adding each individual cell
to the listbox. Seems cumbersome and inefficient.
Have I missed something?

Line Input #1, dataline
Call assignrecord(dataline, temprecord)
listrecords.AddItem temprecord(0)
recordindex = listrecords.ListCount - 1
ReDim Preserve surveyrec(6, recordindex)
For I = 1 To 6
listrecords.List(recordindex, I) = temprecord(I)
temprecord(I) = "" 'empty temp array
Next
Any (positive) criticism or advise gratefully accepted.

Regards,

Jon Rasmussen
0 Likes
Message 5 of 9

Anonymous
Not applicable
Read the contents of the file into either a collection or scripting
dictionary. Then convert the col/dic to a dynamic array.

___________________________
Mike Tuersley
AutoCAD Clinic
Rand IMAGINiT Technologies
0 Likes
Message 6 of 9

Anonymous
Not applicable
Thanks Mike.

Jon Rasmussen
0 Likes
Message 7 of 9

Anonymous
Not applicable
Hi Jon,

An alternative to Mike's suggestion is to write the data to the listbox as
you read it. Unless you need it sorted, it can simply stay there.

Once you've finished reading the file, you read it direct from the listbox
with code like that below:

Private Sub UserForm_Initialize()
UserForm1.ListBox1.AddItem "123"
UserForm1.ListBox1.AddItem "343"
UserForm1.ListBox1.AddItem "453"
UserForm1.ListBox1.AddItem "463"
Dim vStr As Variant
vStr = ListBox1.List
' Then you can do things like:
Dim i as Integer
For i = LBound(vStr) to UBound(vStr)
..........
..........
Next i
End Sub


--


Laurie Comerford
CADApps
www.cadapps.com.au

"Mike Tuersley" wrote in message
news:MPG.1a29235454dd1f489897bc@discussion.autodesk.com...
> Read the contents of the file into either a collection or scripting
> dictionary. Then convert the col/dic to a dynamic array.
>
> ___________________________
> Mike Tuersley
> AutoCAD Clinic
> Rand IMAGINiT Technologies
0 Likes
Message 8 of 9

Anonymous
Not applicable
You can't.
0 Likes
Message 9 of 9

Anonymous
Not applicable
Bummer.
Thanks for the info.
Jon Rasmussen
0 Likes