Solved

How to align 2nd column in ListBox ?

Posted on 2008-06-09
11
306 Views
Last Modified: 2013-12-26
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.
0
Comment
Question by:bhavesh55
  • 5
  • 5
11 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21745869
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
 
LVL 13

Expert Comment

by:game-master
ID: 21748028


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
 

Author Comment

by:bhavesh55
ID: 21762792
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21764871
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 125 total points
ID: 21764940
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:bhavesh55
ID: 21770099
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
 

Author Comment

by:bhavesh55
ID: 21770180
Hi GrahamSkan,
What is "SP(i)" in following syntax ?
 Combo1.AddItem strFirstName & String(10, " ") & SecondNames(i) & ", " & SP(i)

Please advise.

Thanks.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21770590
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
 

Author Comment

by:bhavesh55
ID: 21770985
Hi GrahamSkan,
Do you know any fixed width font which looks similar to 'MS Sans Serif' font than i can resolve issue.

Thanks.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21771172
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
 

Author Closing Comment

by:bhavesh55
ID: 31465511
Hi GrahamSkan, Thanks a lot for your time and help.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now