Solved

formated list box

Posted on 2000-04-27
11
232 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:csindorf
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 1

Expert Comment

by:yfang
ID: 2757211
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
 
LVL 32

Expert Comment

by:bhess1
ID: 2757237
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
 

Expert Comment

by:richking042700
ID: 2757460
If you want, you can set the listbox Columns property to 2 and this would split the list box into two columns.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:csindorf
ID: 2759398
Adjusted points from 20 to 30
0
 

Author Comment

by:csindorf
ID: 2759399
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
 

Author Comment

by:csindorf
ID: 2759795
Adjusted points from 30 to 50
0
 

Author Comment

by:csindorf
ID: 2759796
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
 
LVL 32

Expert Comment

by:bhess1
ID: 2759897
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
 

Accepted Solution

by:
richking042700 earned 50 total points
ID: 2759924
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
 
LVL 32

Expert Comment

by:bhess1
ID: 2759961
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
 
LVL 1

Expert Comment

by:yfang
ID: 2761098
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

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question