PDA

View Full Version : [SOLVED:] Aligning Items in Listbox



Jfp87
05-01-2015, 12:30 AM
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

gmaxey
05-01-2015, 04:39 AM
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

gmayor
05-01-2015, 04:41 AM
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 :)

Jfp87
05-01-2015, 06:08 AM
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.