How to align 2nd column in ListBox ?

Dear Experts,
I would like to display 2 column's value in listbox.Following is my code:


    n = 1
    Do While rst.EOF = False
        cControl.AddItem rst!FName & vbTab & vbTab & vbTab & RTrim(rst!SName)
        rst.MoveNext
        n = n + 1
    Loop


It is displaying following values:
Arif      Ben
Sangakara      kumaran
Tom      Martin


You can see the "SName" values are not aligned properly.

Please advise how to align second column's values.

Thanks.
bhavesh55Asked:
Who is Participating?
 
GrahamSkanConnect With a Mentor RetiredCommented:
Another way is to use a Font where each character takes up the same space. The number of padding spaces can easily be adjusted to  the length of the word. It is more accurate, but there aren't very many fixed-width fonts. This code sets it to Courier New
Option Explicit
 
Private Sub Command1_Click()
    Dim strFirstName As String
    Dim FirstNames(2) As String
    Dim SecondNames(2) As String
    Dim i As Integer
    Dim w As Single
    
    FirstNames(0) = "Arif"
    SecondNames(0) = "Ben"
    FirstNames(1) = "Sangakara"
    SecondNames(1) = "Kumaran"
    FirstNames(2) = "Tom"
    SecondNames(2) = "Martin"
    
    'Find width of widest first name
    For i = 0 To 2
        If w < Len(FirstNames(i)) Then
            w = Len(FirstNames(i))
        End If
    Next i
    
    'Add first name, padding & second name
    Combo1.Font.Name = "Courier New"
    Combo1.Clear
    For i = 0 To 2
        strFirstName = FirstNames(i) + String(w - Len(FirstNames(i)), " ")
        Combo1.AddItem strFirstName & " " & SecondNames(i)
    Next i
End Sub

Open in new window

0
 
GrahamSkanRetiredCommented:
You can try measuring the width of the first name and padding it out with spaces. Note that the Form has a Textwidth method, so the form's and combo's font should be the same.


Option Explicit
 
Private Sub Command1_Click()
    Dim strFirstName As String
    Dim FirstNames(2) As String
    Dim SecondNames(2) As String
    Dim i As Integer
    Dim w As Single
    
    FirstNames(0) = "Arif"
    SecondNames(0) = "Ben"
    FirstNames(1) = "Sangakara"
    SecondNames(1) = "Kumaran"
    FirstNames(2) = "Tom"
    SecondNames(2) = "Martin"
 
    'Find width of widest first name
    For i = 0 To 2
        If w < Me.TextWidth(FirstNames(i)) Then
            w = Me.TextWidth(FirstNames(i))
        End If
    Next i
    
    'Add first name, padding & second name
    Combo1.Clear
    For i = 0 To 2
        strFirstName = FirstNames(i)
        Do While Me.TextWidth(strFirstName) < w
            strFirstName = strFirstName & " "
        Loop
        Combo1.AddItem strFirstName & " " & SecondNames(i)
    Next i
End Sub

Open in new window

0
 
game-masterCommented:


good morning...

if you really wanted to align it correctly..i would rather suggest you
to use the listview control..it is very good and very easy to use...

here is the code...

'add listview control on ur form and name it "listview1" for exmaple..
'right click the listview control and add column for "FIRST Name" and "Surname"
'set the the listview "VIEW" property to "lvwReport" this style make your listview
show even the header of the column...

to add data from database...

sub AddListViewData()

    n = 1
    Do While rst.EOF = False
        with listview1.listitems.add(rst!Fname)   'first column data..
               with .listsubitems.add(rst!Sname)    'second column data..
               end with
        end with
        rst.MoveNext
        n = n + 1
    Loop

end sub


im glad i could help!...


game-master

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
bhavesh55Author Commented:
Hi game-master,
Thanks for your time but in order to change control i have to change so many thing.

Hi Graham,
In first 3 rows, 2nd names are one space behind than in last 2 rows.
e.g.

A       XYZ
B        ABC
C        ABC
D      ABC
E      ABC

Could you please advise if you have any idea how to fix this bug ?

Thanks.
0
 
GrahamSkanRetiredCommented:
It isn't exactly a bug. The code was to workaround the fact that there is no tabbing and it is difficult to work out how much space to add with proportional fonts. Unfortunately the space character has a fixed width, so it can only be accurate to the nearest whole number of spaces.

Perhaps a larger minimum gap would make it less noticeable.

        Combo1.AddItem strFirstName & String(10, " ") & SecondNames(i) & ", " & SP(i)
0
 
bhavesh55Author Commented:
Hi GrahamSkan,
In the last code you provided,
Instead of "Courier New" font can you make it work for "MS Sans Serif" font ?

Please advise.

Thanks.
0
 
bhavesh55Author Commented:
Hi GrahamSkan,
What is "SP(i)" in following syntax ?
 Combo1.AddItem strFirstName & String(10, " ") & SecondNames(i) & ", " & SP(i)

Please advise.

Thanks.
0
 
GrahamSkanRetiredCommented:
Two sorries.

No. 'MS Sans Serif' is a proportional font, where each letter only takes the space that is needed. An "i" is not so wide as an "m", so it is not given the same space on the line. Courier New is a fixed-width font, where each letter takes up the same space. In fact It is the default font in VB because alignment is important in programming.

The sp(i) was something I used in debugging. I didn't notice that I had left it on the end. It is not needed. This bit: & String(10, " ") was what I what talking about in the text, so that the line should read:

Combo1.AddItem strFirstName & String(10, " ") & SecondNames(i)
0
 
bhavesh55Author Commented:
Hi GrahamSkan,
Do you know any fixed width font which looks similar to 'MS Sans Serif' font than i can resolve issue.

Thanks.
0
 
GrahamSkanRetiredCommented:
VB (up to version 3) once used a font called Fixedsys (or perhaps FixSys), but it doesn't seem to be on my PC any more. The only other one that I can find on my system is Lucida console.

You can sample other fixed-width fonts here:

http://www.lowing.org/fonts/
0
 
bhavesh55Author Commented:
Hi GrahamSkan, Thanks a lot for your time and help.
0
All Courses

From novice to tech pro — start learning today.