Click to See Complete Forum and Search --> : Listbox columns
comart
April 19th, 2001, 12:00 PM
I want to display two fields of varying lengths in a listbox and have them line up nicely rather than randomly in a zigzag fashion. The columns property doesn't give me what I'm trying to accomplish. Is it possible to space the data entries in a listbox according to some rule?
softweng
April 19th, 2001, 12:10 PM
Here is some code I used to Create 2 columns in a listview. Note that I had to add 3 columns and
set the first one so you couldn't see it to get it to work right.
private Sub SetupChart()
Dim colDummy as ColumnHeader
Dim colCode as ColumnHeader
Dim colPart as ColumnHeader
Dim i as Integer
on error GoTo ErrHandler
'//Clear Contents Of ListView
lvwKeyCode.ColumnHeaders.Clear
lvwKeyCode.ListItems.Clear
'//Initialize Column Headers
set colDummy = lvwKeyCode.ColumnHeaders.Add()
set colCode = lvwKeyCode.ColumnHeaders.Add()
set colPart = lvwKeyCode.ColumnHeaders.Add()
'//Define Column Properties
'//Must Have A Dummy Column So That KeyCode Column Can Be Centered (VB Bug)
colDummy.Width = 1
'//KeyCode Column
colCode.Text = "KeyCode"
colCode.Width = 825
colCode.Alignment = lvwColumnCenter
'//Associated Column
colPart.Text = "Part Number"
colPart.Width = lvwKeyCode.Width - (colCode.Width + 100)
colPart.Alignment = lvwColumnCenter
'//Fill ListView With Data
for i = 1 to 8
lvwKeyCode.ListItems.Add , , i
lvwKeyCode.ListItems(i).ListSubItems.Add , , txtKeyCodes(i - 1).Text
lvwKeyCode.ListItems(i).ListSubItems.Add , , cmbParts(i - 1).Text
next i
Exit Sub
ErrHandler:
ProccessError ("frmCharts.SetupChart")
End Sub
Kris
Software Engineer
Phoenix,AZ
dfwade
April 19th, 2001, 12:20 PM
take a look at this site
http://www.thescarms.com/VBasic/listbox.asp
Iouri
April 19th, 2001, 12:30 PM
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const LB_SETTABSTOPS As Long = &H192
Private Sub Form_Load()
Dim lTabs(2) As Long
lTabs(1) = 30
lTabs(2) = 60
SendMessage List1.hwnd, LB_SETTABSTOPS, 3&, lTabs(0)
List1.AddItem "one" & vbTab & "two" & vbTab & "three"
List1.AddItem "four" & vbTab & "five" & vbTab & "six"
End Sub
Iouri Boutchkine
iouri@hotsheet.com
Iouri
April 19th, 2001, 12:31 PM
Here is another example
' Description:Fills a combo box with fields returned from a query, lining up each column. No limit to number of columns returned.
' Inputs:dbs As Database, strSQL As String, ctl As Control, Optional intNumSpaces As Integer
Function FillListBox(dbs As Database, strSQL As String, ctl As Control, Optional intNumSpaces As Integer) As Integer
Dim rst As Recordset
Dim intN As Integer
Dim intR As Integer
Dim strItem As String
Dim intTemp As Integer
Dim intMaxLength() As Integer
If Not IsMissing(intNumSpaces) Then intNumSpaces = 3
Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)
rst.MoveLast: rst.MoveFirst
'For each field in the recordset, find the longest string. This will be used to line up spacing
ReDim intMaxLength(rst.Fields.Count)
For intN = 0 To rst.Fields.Count - 1
rst.MoveFirst
intMaxLength(intN + 1) = Len(rst.Fields(intN))
For intR = 1 To rst.RecordCount
If Len(rst.Fields(intN).Value) > intMaxLength(intN + 1) Then
intMaxLength(intN + 1) = Len(rst.Fields(intN).Value)
End If
rst.MoveNext
Next
Next
'Add each record's field(s) to the list.
rst.MoveFirst
For intR = 1 To rst.RecordCount
strItem = ""
For intN = 0 To rst.Fields.Count - 1
strItem = strItem & rst.Fields(intN).Value & Space((intMaxLength(intN + 1) + intNumSpaces) - Len(rst.Fields(intN)))
intTemp = 0
Next
strItem = Trim(strItem)
ctl.AddItem strItem
rst.MoveNext
Next
End Function
Iouri Boutchkine
iouri@hotsheet.com
comart
April 19th, 2001, 12:47 PM
I cannot get this to work. Is something left out of the code?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.