formated list box

Is there a way to format a list box so there are 2 columns of items the right side is right justified and the left side is left justified?

Craig
csindorfAsked:
Who is Participating?
 
richking042700Connect With a Mentor Commented:
Switch to a Mono space font.  Then you pad the variable.

Example:

'USE a Mono space font like Courier
Private Sub Form_Load()

    Dim strStringOne As String * 15
    Dim strStringTwo As String
   
    'Quick little example just to show you.
    For i = 1 To 10
        strStringOne = "Apples" & i * i
        strStringTwo = strStringOne & CStr(i)
        List1.AddItem strStringTwo
    Next
   
End Sub

0
 
yfangCommented:
The first method I would try is to put spaces between the two items ("item1" & "a number of spaces" & "item2") before you populate the listbox.  It should be easy to determine how many spaces you need between any specific pair of items, based on the width of the listbox (list1.width), the lengths of the items (len("item1"), len("item2")).  Then you can use a loop to add the number of spaces.
0
 
Brendt HessSenior DBACommented:
Check this reference:

http://www.thescarms.com/vbasic/listbox.htm

It provides a sample of setting tab stops, including right justified tab stops, in a listbox.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
richking042700Commented:
If you want, you can set the listbox Columns property to 2 and this would split the list box into two columns.
0
 
csindorfAuthor Commented:
Adjusted points from 20 to 30
0
 
csindorfAuthor Commented:
If I set the property to 2 columns then how do I enter the items into them and right and left justify?  Because the items going into the columns might be different lengths so I would want the first letter of the first item on the far left and the last letter of the other item on the far right.

Info:  the first column will be a list of part numbers then in the right column will be there one word description.  ex:

123445        washer
53453466         nut
23d          ferrule
0
 
csindorfAuthor Commented:
Adjusted points from 30 to 50
0
 
csindorfAuthor Commented:
Question only partially answered.  I saw the column property.  But not a way to left and right justify.

Could yfang go into a bit more detail please?  I have not been into VB for 2 years now but my new contract has put me into the mode again to learn and cram fast.

A bit more info is that I will be hitting a Database and pulling out part numbers and their descriptions.  I then need to put them into a list box.  The formatting is an extra bonus I am trying to do. So when the user looks at the list there are spaces or tabs between the items, which can be different lengths.

TIA
Craig
0
 
Brendt HessSenior DBACommented:
There is no way without using SendMessage to right justify a column in a list box.

Did you check the link I referenced above on how to set right justified tab stops in a listbox?
0
 
Brendt HessSenior DBACommented:
One other possibility:  The free CoolTabs control from the Common Control Replacement Project's CoolTools collection at:

http://www.mvps.org/ccrp/download/cooltools.htm

To look over all of there controls, go to their home page:

http://www.mvps.org/ccrp/index.html

Really useful stuff to be found here!
0
 
yfangCommented:
Here is a simple way to to it:

1. Set the font of your listbox to "Courier" since with this font all the characters are the same fat.
2. One your listbox is drawn, you can test how many characters it will hold.
3. Code similar to this:

Private Sub Command1_Click()
 Dim str1 As String, str2 As String
 Dim str As String
 Dim LengthList As Integer
 list1.Font = "Courier"
 str = "abcdefghijklmno"
 LengthList = LenB(str)
 list1.AddItem str
 str1 = "test1"
 str2 = "terere2"
 str = str1
 Do
    str = str & " "
 Loop Until LenB(str) = Fix((LengthList - LenB(str2)))
 str = str & str2
 list1.AddItem str

End Sub

Note: the Loop ... and ((LenthList ...
actually belong to the same line.  If you have any problem with this, feel free to email me at: yfang@lawco.com

Good luck!

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.