Consulting

Results 1 to 4 of 4

Thread: Aligning Items in Listbox

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location

    Aligning Items in Listbox

    Guys,

    Each item which I add to my listbox is a group of concatenated strings, each string holds a different piece of information about a document. Although this information is being added to the listbox as a single item, I am trying to create some alignment between each of the individual strings which make up the item to be added. The reason for this is that the length of the strings vary widely from one document to the next, which gives it a messy layout in the listbox.

    I have already produced this crude solution which evaluates the length of each string to be concatenated and then applies a certain number of tabs (which works for every occurence so far) but is there a better way, or something I have completeley overlooked?

    arrDoc(i) = cboDocType.Value
    arrDiscip(i) = cboDiscip.Value
    arrNo(i) = cboNo.Value
    arrSub(i) = txtSub.Value
    arrDocNum(i) = txtProjNo.Value & "-" & cboDocType.Value & "-" & cboDiscip.Value & cboNo.Value
    If Len(arrDocNum(i)) = 16 Then
        arrDocNum(i) = arrDocNum(i) & vbTab & vbTab & vbTab & strDocTitle
    Else
        arrDocNum(i) = arrDocNum(i) & vbTab & vbTab & strDocTitle
    End If
    If Len(strDocTitle) <= 7 Then
        arrDocNum(i) = arrDocNum(i) & vbTab & vbTab & vbTab & vbTab & txtSub.Value
    ElseIf Len(strDocTitle) > 7 And Len(strDocTitle) <= 14 Then
        arrDocNum(i) = arrDocNum(i) & vbTab & vbTab & vbTab & txtSub.Value
    ElseIf Len(strDocTitle) > 14 And Len(strDocTitle) <= 23 Then
        arrDocNum(i) = arrDocNum(i) & vbTab & vbTab & vbTab & txtSub.Value
    ElseIf Len(strDocTitle) > 23 And Len(strDocTitle) <= 30 Then
        arrDocNum(i) = arrDocNum(i) & vbTab & vbTab & txtSub.Value
    ElseIf Len(strDocTitle) > 31 And Len(strDocTitle) < 33 Then
        arrDocNum(i) = arrDocNum(i) & vbTab & vbTab & txtSub.Value
    ElseIf Len(strDocTitle) >= 33 Then
        arrDocNum(i) = arrDocNum(i) & vbTab & txtSub.Value
    End If
    ListBox1.AddItem arrDocNum(i)
    Thanks guys

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Why don't you use a multi-column listbox?

    Dim arrDocNum() As String
    Dim arrParts() As String
    Dim strDocTitle As String
    Dim lngIndex As Long
    ReDim Preserve arrDocNum(0)
    arrDocNum(0) = "AAAAAAAAAAA-BBBBBBBBBBBBBBB-CCCCCCCCCCCCCCCC-DDDDDDDDDDDD"
    strDocTitle = "Test Title"
    arrDocNum(0) = arrDocNum(0) & "-" & strDocTitle
    arrParts = Split(arrDocNum(0), "-")
    With ListBox1
      .ColumnCount = UBound(arrParts) + 1
      .AddItem
      For lngIndex = 0 To UBound(arrParts)
        .List(.ListCount - 1, lngIndex) = arrParts(lngIndex)
      Next
    End With
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    You might be able to consider using a multi-column list box and display only one of the columns, then re-assemble the data from the columns for the selected item before using it.

    P.S It seems Greg had the same idea at the same time
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location
    Thanks guys

    For some reason I previously thought I couldn't use the multi-column listbox. I will need to go away and have a try with your code gmaxey...then ask the 2nd part of my question. Thanks again.

Posting Permissions

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