Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to align 2nd column in ListBox ?

Posted on 2008-06-09
11
Medium Priority
?
321 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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
 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

704 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