The Student Room Group
Reply 1
flukey2005
How in vb how do i populate a list box which has multiple columns
I know how to add items e.g. List1.AddItem "Hello" but i dont know to do it for multiple columns.

Any help would be greatly appreciated. Rep is on offer.

F :smile:


If you need to seperately select the items in the list box - afraid cannot help, other than to suggest using a second list box!

Otherwise, would it not just be a case of seperating the string by a defined number of spaces (having done a character count on the first text item)?
Reply 2
I'm straight copying from a website here, not a VB6 coder.

The standard "Open" dialog box, which most applications have, is an example of a ListBox with multiple columns. Setting the Columns property to the number of columns desired will create this for you and horizontal scroll bars are automatically added when the list doesn't entirely fit.

Setting the number of columns might not give the results you expect. For instance, if you have 10 list items and 5 columns, you would think each column would contain 2 items. But it all depends on the width and height of the ListBox. If you draw the ListBox with enough Height to fit all the items in one column, then that's what you'll get, one long column and four blank ones. Also important to remember is that the width of one column is equal to the width of the ListBox divided by the number of columns.


Width and Height the reason it isn't appearing in multiple columns for you? tried VB6 BlackBook? Apparently people swear by it.
Reply 3
UniqueLilMe
I'm straight copying from a website here, not a VB6 coder.



Width and Height the reason it isn't appearing in multiple columns for you? tried VB6 BlackBook? Apparently people swear by it.

Nah its not that, i have the list box shape/size etc.. working fine, i just dont know the syntax to populate items in multiple columns.

Thanks for your help.

ps. SirBob there must be a way otherwise it is pointless of having the property
Reply 4
Nah haven't tried VB BlackBook, shall look into that, i have VB6 complete which is like 1100 pages long but it mentions nothing about multiple columns population.
Reply 5
Nah its not that, i have the list box shape/size etc.. working fine, i just dont know the syntax to populate items in multiple columns.


From what I gathered from that other one, the syntax should be the same. [List1.AddItem ("Item 0") etc]But the fact that the box is too small to hold all items in one column and the fact that multiple columns are enabled [via Columns property of the list] means it spreads into multiple columns?? That's the impression I seem to be getting from VB6 blackbook as well [I borrowed it from someone here for a sec]

It seems to use the same code example for single column as multiple column but adds:

"If you've made list1 large you might have to make it smaller before it will display the items in a number of columns rather than one large column".
Its exactly the same, you just do .Add(Whatever)

The only difference is that a listbox when its made smaller will condense everything into multiple columns if its enabled, and THEN begin scrolling when it runs out of room.

You can't really specify which column things are going to be on unless you start messing around with the onscroll events and resize the box when you add an item that pops onto another column

[edit]

And its not really VB you should be looking at specifically. Look it up on MSDN, .NET is cross-language.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformslistboxclasstopic.asp
Reply 7
Dero
Its exactly the same, you just do .Add(Whatever)

The only difference is that a listbox when its made smaller will condense everything into multiple columns if its enabled, and THEN begin scrolling when it runs out of room.

You can't really specify which column things are going to be on unless you start messing around with the onscroll events and resize the box when you add an item that pops onto another column

[edit]

And its not really VB you should be looking at specifically. Look it up on MSDN, .NET is cross-language.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformslistboxclasstopic.asp

VB6 isn't .net?
No, but all the class structures are the same.

I didn't really mean to imply that VB6 itself was :wink:
Reply 9
i don know if anyone is still reading this but i will post it for the googlers that might come across this page.

I ran across a quick population of a columned list box on google the other day it looked like this.
this is a copy of the code. i got it from:
http://www.ilook.fsnet.co.uk/vb/vblbtab.htm


Private Declare Function SendMessageArray Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Const LB_SETTABSTOPS = &H192

' Create 3 tab stops in our list box (gives 4 columns), then populate the tabs
' with some values
Private Sub Command1_Click()
Dim i As Integer
ReDim LBTab(1 To 3) As Long
LBTab(1) = 30
LBTab(2) = 70
LBTab(3) = 110
' Set tab stops, params are Hwnd of list box, number of tabs in array, tab settings array
SendMessageArray List1.hwnd, LB_SETTABSTOPS, 3, LBTab(1)
' Populate listbox with some test data - use vbtab to separate columns
For i = 1 To 10
List1.AddItem "Item" & i & vbTab & "Item" & (i * 5) & vbTab & "item" & (i * 11) & vbTab & "item" & (i * 19)
Next
End Sub
Reply 10
Yeah my method is just using a VBtab

List1.additem FirstItem & vbtab & SecondItem etc..
If you're trying to use columns for conceptually different data - say, names and ages - you should be looking at using a grid control rather than a ListBox.
Reply 12
Use a listview control. Listbox I think only has the ability for multiple columns like in a newspaper: When one column runs out it moves on to the next. To create a table of info use a listview control

Articles for you