PDA

View Full Version : Create and populate listbox columns and rows with text strings...



Coolmo
2006-10-20, 02:00 PM
How on earth do you create and populate listbox columns and rows with text strings and place headers at the top at run time? I'm trying to get away from using a flexgrid control because you have to make sure the user has the OCX on their computer in order for it to work and that's becoming a problem lately. I hope that a listbox with columns can do almost the same thing. Can someone point me in the right direction with some simple code to do this? I've only reached the point of placing 6 columns in the listbox at design time but ".additem" only places text in the first column (at least that's what appears to be happening). Any help is appreciated. Thanks!

Ed Jobe
2006-10-20, 02:46 PM
Correct, the AddItem method only populates the first column. After the row is added, fill in the additional columns as this snippet shows.


lbxTitleBlock.AddItem strName
lbxTitleBlock.List(i - 1, 1) = strFile
lbxTitleBlock.List(i - 1, 2) = strMaj
lbxTitleBlock.List(i - 1, 3) = strMIn
lbxTitleBlock.List(i - 1, 4) = strGUID

The listbox control does not have a column header like a datagrid. I suppose you could just populate the first row with column names. I just use trial and error to position labels above the listbox.

Coolmo
2006-10-20, 04:06 PM
My listbox control (out of the box for AutoCAD 2006) has a "columnheader" Boolean property that allows them. They show up on top as blank boxes for now because I have no idea how to fill them in. When I use "additem" it places the text on the first row below the header line but it's still index "0".

In your example, are you saying to "additem" the first column text first and then add the code you specified? I'm use to simply using Listbox1.additem "textstring". What is all the ibxTitleblock.list and strFile, strMaj stuff? Are these names and variables set by you or is this VBA code? Thanks for all the help by the way.

Opie
2006-10-20, 04:17 PM
In Ed's example, I would assume the lbxTitleBlock is the Listbox control. The .list is a property on the Listbox control. The strFile, strMaj stuff appear to be variables.

HTH

Ed Jobe
2006-10-20, 06:28 PM
The first line adds the item with text for the first column (index 0). The successive lines add values to each column in the row, as controlled by the var i.

Coolmo
2006-10-20, 08:41 PM
Wow! That works great! Especially for what I need it for. One more quick question though. I can populate the first row but how do I populate the second row with a new set of values? If I used:

ListBox1.List(i - 0, 1) = "Textstring"

..what does the "(i - 0,1) mean? I assumed the "0" was the index for the row and the "1" is the next column to put "Textstring" in but when I tried to add the next row by adding an item first and then using:

ListBox1.List(i - 1, 1) = "Textstring2"

...I get an error for an invalid array. Thoughts?

Ed Jobe
2006-10-20, 09:06 PM
The list is a 2 dimensional array. The first expression identifies the row, and the second expression identifies the column. Listing the columns is easy. You just need to supply a fixed integer corresponding to your column (zero based). For the row, you need to keep track of what row will be current when you perform the AddItem method. "i - #" is what I needed based upon the context of my code.

fixo
2006-10-21, 03:02 PM
Try this simle example and I hope that
maybe this would be interesting to somebody else :)

~'J'~