CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Listbox columns

  1. #1
    Join Date
    Aug 2000
    Posts
    265

    Listbox columns

    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?


  2. #2
    Join Date
    Mar 2000
    Location
    Arizona, USA
    Posts
    493

    Re: Listbox columns

    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
    Kris
    Software Engineer
    Phoenix, AZ USA

  3. #3
    Join Date
    Aug 2000
    Location
    KY
    Posts
    766

    Re: Listbox columns


  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Listbox columns

    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
    [email protected]
    Iouri Boutchkine
    [email protected]

  5. #5
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Listbox columns

    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.

    ' Inputsbs 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
    [email protected]
    Iouri Boutchkine
    [email protected]

  6. #6
    Join Date
    Aug 2000
    Posts
    265

    Re: Listbox columns

    I cannot get this to work. Is something left out of the code?


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured